Php is web programming language very popular among developers. Php is actually a scripting language which means there is no need to compile the code to run. So write and run is the motto of php language. Php scripts can be run on Linux systems like bash, Perl scripts too. Some administrators with development background prefers php for scripting because php offers more than bash scripting. SQlite is an other popular database which is designed for simple, reliable and fast usage. Sqlite do not need any setup and service to run. Just SQlite library and create a database file like Microsoft Access mdb and work. In this tutorial we will look how to create simple and fast php application in bash which uses sqlite database for storing data.

Install Php & Sqlite

We will install required PHP and SQlite package with the following commands.

Ubuntu, Debian, Mint

$ sudo apt install php sqlite

Fedora,CentOS, RedHat

$ sudo yum install -y php sqlite

Test Php

Php installation can be tested like below. We will run simple php code with PHP interpreter.

$ php -r "echo 'Hi Poftut';"

We can run php as above

php is our Php interpreter command

is our Php interpreter command -r will run our code “echo ‘Hi Poftut’; ”

Initialize Sqlite Database With Php

We will create a database file with php script. And then create a table. Our php script file name is createdb.php

<?php $db = new SQLite3('mysqlitedb.db'); if($db->exec('CREATE TABLE bar (bar STRING)') ){ echo "Successfully Created"; } else{ echo "Failure"; } ?>

Run our createdb.php script like below

$ php createdb.php Successfully Created

If we look in the current working directory we can see that there is a file named mysqlitedb.db

Run Application

Our application will insert specified parameter into our database. Our application code is resides in insertdata.php . We insert data into table named bar with the following script.

<?php $db = new SQLite3('mysqlitedb.db'); if($db->exec('Insert Into bar values ("'+$argv[1]+'");') ){ echo "Successfully Created"; } else{ echo "Failure"; } ?>

We can run our code like below

$ php insertdata.php "hi" Successfully Created

How To Create Fast Simple Database and Table With Php and Sqlite Infografic