First check your php installation by using phpinfo()
<?Php
echo phpinfo();
?>
You should see the details about the SQLite3 like this if the same is available.
If sqlite3 support is not available then it is to be enabled by opening the PHP configuration file, php.ini and un-comment the extension.
Inside the extension directory in your system you must have php_sqlite3.dll in order to use this extension.
Connecting and displaying records of SQLite Database by SQLite3 using PHP Script - 18
Connection to SQLite database
SQLite3 is a serverless, file-based database system. When connecting to a SQLite3 database, we must specify the path to the database file.
If the file exists, SQLite3 will open it and establish a connection. If the file does not exist, SQLite3 will create a new database file with the given name.
You can use any extension you want for the database file, but it is common to use the `.db` extension.
//$my_conn = new SQLite3('my_db.db');// same path as file execution
//$my_conn = new SQLite3('D:\\my_db\\my_db.db');// Full path with file name
$my_conn = new SQLite3(dirname(__FILE__).'/my_db.db'); // same location
Once our connection is established we can collect some records from a table. Note that here we already have student table with some records.
$my_conn = new SQLite3('my_db.db'); // connection to db
$sql="SELECT * FROM student "; // Query to collect records
echo "<table>";
$results = $my_conn->query($sql);
while ($row = $results->fetchArray()) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td></tr>";
}
echo "</table>";
You can download the sample SQLite database with student table from here also.
backup() returns true on success. We will create one backup database using our main database.
$my_conn = new SQLite3('my_db.db');// Connect to existing database
$my_conn_backup = new SQLite3('D:\\my_db\\backup.db'); // location with file name
if($my_conn->backup($my_conn_backup)){
echo " Backup database is created ";
}else{
echo " Failed to create backup database ";
}
sqlite3_changes()
This function is a useful tool for working with SQLite databases. It can be used to verify that queries are executed successfully.