SQLite

New in PHP version 5 is the compact and small database connection called SQLite. As its name suggests, it is a small and lightweight database tool. This database product comes with PHP 5 and is now available in PHP by default. SQLite is ready to go right out of the box when you install PHP, so if you are looking for a lightweight and compact database tool, be sure to read up on SQLite.

The catch with SQLite is that all the database storage is file-based, and is therefore accomplished without the use of a separate database engine. This can be very advantageous if you are trying to build an application with a small database footprint and without product dependencies other than PHP. All you have to do to start using SQLite is to make reference to it in your code.

Note

If you are using PHP 5.3, you may have to update your php.ini file to include the directive extension=php_sqlite.dll, since at the time of this writing, the default directive of extension=php_sqlite3.dll does not seem to have the same working content.

There is an OOP interface to SQLite, so you can instantiate an object with the following statement:

$db = new SQLiteDatabase("c:/copy/library.sqlite");

The neat thing about this statement is that if the file is not found at the specified location, SQLite creates it for you. Continuing with our library database example, the command to create the authors table and insert a sample row within SQLite would look something like Example 8-2.

Example 8-2. SQLite library authors table ...

Get Programming PHP, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.