Creating a Database Connection with SQLite3 using PHP Tutorial

Introduction

In this tutorial, I will show you how to Create a Database Connection with SQLite3 in PHP using the Object-Oriented Programming (OOP) Approach. I will also teach you the basic way of creating a database table in SQLite3 in PHP. The tutorial aims to provide students new to PHP Language a reference to learn about PHP and SQLite3 Integration. Here, snippets and sample scripts will be provided.

What is SQLite3

SQLite3 is a C-Language Library SQL Database Engine that is compact, quick, self-contained, highly reliable, and fully featured. It is built into mobile or smartphones, most computers or laptops, and many other applications. It is one of the most popular database engines in the world.

How to enable SQLite3 in PHP?

SQLite3 already comes with PHP 5.3.0 or later versions which means you don't need to download it. Although you still need to enable the SQLite3 extension in your PHP to be able to use it. To enable it to follow the instruction below.

  1. Locate your PHP's php.ini file.
  2. Open the file with your preferred text editor such as Notepad++, Sublime text, etc.
  3. Locate the SQLite3 extension by searching the line with extension=sqlite3.
  4. Remove the semi-colon (;) located before the extension word to uncomment the line or enable the said extension.
  5. Make sure to restart your web servers such as Apache or Nginx.

php.ini - sql3 extension

How to create a Database and DB Connection in SQLite3 using PHP OOP?

Here are the following snippets on how to create SQLite Database and connect with it in PHP using the Object-Oriented Programming approach.

Creating the Database Connection Class

SQLite3 Database file will be automatically created if not existed yet upon opening a database connection with the given database file name. Here is the sample snippet to do so.

  1. <?php
  2. Class Database extends SQLite3 {
  3. public function __construct($db_file){
  4. // Open SQlite DB Connection
  5. $this->open($db_file);
  6.  
  7. }
  8.  
  9. public function __destruct(){
  10. // Close SQlite DB Connection
  11. $this->close();
  12. }
  13. }

Run the given class with the database path name as the parameter. See the example below

  1. <?php
  2. // Database Directory
  3. $db_dir = "database/";
  4. // Create directory if not exists
  5. if(!is_dir($db_dir))
  6. mkdir($db_dir);
  7.  
  8. // Database pathname
  9. $db_file = $db_dir."dummy_db.db";
  10.  
  11. // Database Connection
  12. $connection = new Database($db_file);

After that, try to run or browse the PHP Script in your web browser to execute the script. If no error occurred, the database is successfully created at the given database path.

Creating Database Table

Next, try to create a new database table on your newly created database. To do that, you will need to use the SQLites' exec method. using the said method you can execute the Schema of your table. Here are sample snippets that demonstrate the table creation in SQLite Database using PHP.

  1. <?php
  2. $this->exec("CREATE TABLE IF NOT EXISTS `user_table` (
  3. `first_name` varchar(250) NOT NULL,
  4. `middle_name` varchar(250) NOT NULL,
  5. `last_name` varchar(250) NOT NULL,
  6. `email` varchar(100) NOT NULL,
  7. `address` TEXT NOT NULL)
  8. ");

You can put the code along with the __construct() function on your database to run automatically upon the database connection process or in a separate function just like the snippet below.

  1. <?php
  2.  
  3. Class Database extends SQLite3 {
  4. public function __construct($db_file){
  5. // Open SQlite DB Connection
  6. $this->open($db_file);
  7.  
  8. }
  9. public function create_dummy_table(){
  10. $create_table = $this->exec("CREATE TABLE IF NOT EXISTS `user_table` (
  11. `first_name` varchar(250) NOT NULL,
  12. `middle_name` varchar(250) NOT NULL,
  13. `last_name` varchar(250) NOT NULL,
  14. `email` varchar(100) NOT NULL,
  15. `address` TEXT NOT NULL)
  16. ");
  17. /**
  18.   * Table will be only created if it is not existed yet
  19.   */
  20. if($create_table)
  21. return true;
  22. else
  23. return false;
  24. }
  25. public function __destruct(){
  26. // Close SQlite DB Connection
  27. $this->close();
  28. }
  29. }
  30.  
  31.  
  32. // Database Directory
  33. $db_dir = "database/";
  34. // Create directory if not exists
  35. if(!is_dir($db_dir))
  36. mkdir($db_dir);
  37.  
  38. // Database pathname
  39. $db_file = $db_dir."dummy_db.db";
  40.  
  41. // Database Connection
  42. $connection = new Database($db_file);
  43. ?>

DEMO VIDEO

That's the end of this tutorial. I hope this tutorial will help you with what you are looking for and enhance your PHP and SQLite3 Integration knowledge. You can also download the source code zip file I created for this tutorial to do some more experiments. The download button is located below this article.

Explore more on this website for Tutorials and Free Source Codes.

Happy Coding :)

Add new comment