Introduction

Here we will see how to configure PHP 7 and SQLite3 in Windows environment. By default SQLite3 extension comes with PHP 5.3.0 or higher version of PHP. So you don’t need to download it. SQLite3 is not enabled by default in Windows environment. So you must enable it before you use it.

Why do you need SQLite?

SQLite, can also be used as an in-memory database, a small, fast, self-contained, high-reliability, full-featured, SQL database engine. SQLite is the most used database engine in the world. SQLite is built into all mobile phones and most computers and comes bundled inside countless other applications. SQLite file format is stable, cross-platform, and backwards compatible.

Prerequisites

PHP 7.0.15, Apache 2.4

Configuring SQLite3 with PHP

Open php.ini file under <php installation directory>/php-7.0.15 and make sure to have the following lines exactly in the same way. You will find that the following lines already exist in the file but you need to uncomment and update the values.

extension_dir = "<php installation directory>/php-7.0.15/ext"
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
sqlite3.extension_dir = "<php installation directory>/php-7.0.15/ext"

Now you may wonder where do those .dll files exist. So those .dll files exist under the extension_dir or sqlite3.extension_dir.

Connecting to SQLite3

In the above, we have enabled the SQLite3 database to be used with PHP 7. Now we will connect to the SQLit3 database. So create a file called sqlite3_conn.php under <apache http server installation directory>/htdocs/SQLite3 with the following code:

<?php
   class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open('test.sqlite');
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }
?>

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

Testing the Connection

Now, let’s run the above program to create our database test.sqlite in the current directory.

So you will find that test.sqlite database is created under directory
<apache http server installation directory>/htdocs/SQLite3 once you execute the sqlite3_conn.php file. The size of the test.sqlite database is 0 KB.

You will also see the below output on the browser:

configure php 7 and sqlite3 in windows

The default port of Apache http server is 80, here I have changed the port to 8000. If you use the default port then you don’t need to type the port in the URL.

So we got an idea how to configure PHP 7 and SQLite3 in Windows environment.

That’s all. Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *