Configure PHP 7 and SQlite3 in Windows

Introduction

In this tutorial I am going to show you 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.

Related Posts:

Prerequisites

PHP 7.4.3, Apache HTTP Server 2.4

Configuring SQLite3 with PHP

Open php.ini file under <php installation directory>/php-7.4.3 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.4.3/ext"
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
sqlite3.extension_dir = "<php installation directory>/php-7.4.3/ext"

Now you may wonder where do those .dll files exist. So those .dll files exist under the extension_dir or sqlite3.extension_dir. The variable extension_dir can be found in the php.ini file itself and actually points to <php installation directory>/php-7.4.3/ext folder.

Connecting to SQLite3

In the above, I have enabled the SQLite3 database to be used with PHP 7. Now I 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 your 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.

That’s all and hope you got an idea how to configure PHP 7 and SQLite3 in Windows environment.

1 thought on “Configure PHP 7 and SQlite3 in Windows

Leave a Reply

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