Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQLite – PHP

SQLite - PHP

Installation

The SQLite3 extension is enabled by default as of PHP 5.3.0. It can be disabled at compile time using --without-sqlite3 to disable the SQLite3 extension.

Windows users must enable php_sqlite3.dll to use this extension. Since PHP 5.3.0, this DLL has been included in the Windows distribution of PHP.

For detailed installation instructions, it is recommended to check our PHP tutorial and its official website.

PHP Interface API

The following are important PHP programs that can meet your needs for using a SQLite database in a PHP program. If you need more details, please refer to the official PHP documentation.

No. API & Description
1 public void SQLite3::open ( filename, flags, encryption_key )

Open a SQLite 3 database. If the build includes encryption, it will try to use the key.

If the file name filename to ':memory:', then SQLite3::open() will create an in-memory database in RAM, which will only last for the duration of the session.

If the file name filename is an actual device file name, then SQLite3::open() will use this parameter value to try to open the database file. If a file with that name does not exist, a new database file with that name will be created.

The optional flags are used to determine whether to open the SQLite database. By default, it opens with SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE.

2 public bool SQLite3::exec ( string $query )

This routine provides a shortcut for executing SQL commands; the SQL commands are provided by the sql parameter and can consist of multiple SQL commands. This routine is used to execute a query with no result for a given database.

3 public SQLite3Result SQLite3::query ( string $query )

This routine executes an SQL query and returns a result if the query returns one. SQLite3Result object.

4 public int SQLite3::lastErrorCode ( void )

This routine returns the numeric result code of the most recent failed SQLite request.

5 public string SQLite3::lastErrorMsg ( void )

This routine returns the English text description of the most recent failed SQLite request.

6 public int SQLite3::changes ( void )

This routine returns the number of database rows updated or inserted or deleted by the most recent SQL statement.

7 public bool SQLite3::close ( void )

This routine closes the database connection previously opened by SQLite3::open().

8 public string SQLite3::escapeString ( string $value )

This routine returns a string that has been properly escaped for safety in an SQL statement.

Connect to the database

The following PHP code shows how to connect to an existing database. If the database does not exist, it will be created, and finally a database object will be returned.


open('test.db');
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }
?>

Now, let's run the above program to create our database in the current directory test.db. You can change the path as needed. If the database is successfully created, the message shown below will be displayed:


Open database successfully

Create a table

The following PHP code snippet will be used to create a table in the previously created database:


open('test.db');
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }

   $sql =<<exec($sql);
   if(!$ret){
      echo $db->lastErrorMsg();
   } else {
      echo "Table created successfully\n";
   }
   $db->close();
?>

When the above program is executed, it will create the COMPANY table in test.db , and display the message shown below:


Opened database successfully
Table created successfully

INSERT Operation

The following PHP program shows how to create records in the COMPANY table created above:


open('test.db');
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }

   $sql =<<exec($sql);
   if(!$ret){
      echo $db->lastErrorMsg();
   } else {
      echo "Records created successfully\n";
   }
   $db->close();
?>

When the above program is executed, it will create the given records in the COMPANY table and display the following two lines:


Opened database successfully
Records created successfully

SELECT Operation

The following PHP program shows how to retrieve and display records from the previously created COMPANY table:


open('test.db');
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }

   $sql =<<query($sql);
   while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
      echo "ID = ". $row['ID'] . "\n";
      echo "NAME = ". $row['NAME'] ."\n";
      echo "ADDRESS = ". $row['ADDRESS'] ."\n";
      echo "SALARY =  ".$row['SALARY'] ."\n\n";
   }
   echo "Operation done successfully\n";
   $db->close();
?>

When the above program is executed, it will produce the following result:


Opened database successfully
ID = 1
NAME = Paul
ADDRESS = California
SALARY =  20000

ID = 2
NAME = Allen
ADDRESS = Texas
SALARY =  15000

ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY =  20000

ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY =  65000

Operation done successfully

UPDATE Operation

The following PHP code shows how to use the UPDATE statement to update any record, and then retrieve and display the updated record from the COMPANY table:


open('test.db');
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }
   $sql =<<exec($sql);
   if(!$ret){
      echo $db->lastErrorMsg();
   } else {
      echo $db->changes(), " Record updated successfully\n";
   }

   $sql =<<query($sql);
   while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
      echo "ID = ". $row['ID'] . "\n";
      echo "NAME = ". $row['NAME'] ."\n";
      echo "ADDRESS = ". $row['ADDRESS'] ."\n";
      echo "SALARY =  ".$row['SALARY'] ."\n\n";
   }
   echo "Operation done successfully\n";
   $db->close();
?>

When the above program is executed, it will produce the following result:


Opened database successfully
1 Record updated successfully
ID = 1
NAME = Paul
ADDRESS = California
SALARY =  25000

ID = 2
NAME = Allen
ADDRESS = Texas
SALARY =  15000

ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY =  20000

ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY =  65000

Operation done successfully

DELETE Operation

The following PHP code shows how to use the DELETE statement to delete any record, and then retrieve and display the remaining records from the COMPANY table:


open('test.db');
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }
   $sql =<<exec($sql);
   if(!$ret){
     echo $db->lastErrorMsg();
   } else {
      echo $db->changes(), " Record deleted successfully\n";
   }

   $sql =<<query($sql);
   while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
      echo "ID = ". $row['ID'] . "\n";
      echo "NAME = ". $row['NAME'] ."\n";
      echo "ADDRESS = ". $row['ADDRESS'] ."\n";
      echo "SALARY =  ".$row['SALARY'] ."\n\n";
   }
   echo "Operation done successfully\n";
   $db->close();
?>

When the above program is executed, it will produce the following result:


Opened database successfully
1 Record deleted successfully
ID = 1
NAME = Paul
ADDRESS = California
SALARY =  25000

ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY =  20000

ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY =  65000

Operation done successfully