Configuring PHP for SQLite Support


Modern versions of PHP make it very easy to add in SQLite support, so we'll briefly look at the steps required for both Linux/Unix and Windows versions of PHP.

For more general assistance with PHP configuration on a particular platform, refer to the online documentation at http://www.php.net/manual/en/installation.php.

Configuring PHP for Linux/Unix

From PHP 5, the SQLite extension is bundled with the PHP distribution and will be enabled at compile time unless it is disabled with the --without-sqlite option.

You do not need to install any SQLite components before building PHP, but you will need to download the sqlite tool separately if you want to use it to examine your databases outside of PHP.

For earlier PHP versions, the official extension can be obtained from http://pecl.php.net/package/SQLite. This also includes source for libsqlite, so again there are no other prerequisites for adding the module to PHP. With PHP 4.3.0 and later, you can use the pear utility to automatically download and install the SQLite extension:

 # pear install sqlite downloading SQLite-1.0.2.tgz ... Starting to download SQLite-1.0.2.tgz (362,412 bytes) .........................................done: 362,412 bytes 51 source files, building running: phpize [...] install ok: SQLite 1.0.2 

The full output is quite lengthy, showing that the latest version of the extension has been downloaded, compiled, and copied to your PHP extensions directory.

For earlier versions of PHP, you can download and compile the extension by hand after downloading the latest version by following these steps:

 # gzip d SQLite-1.0.2.tar.gz # tar xf SQLite-1.0.2.tgz # cd SQLite-1.0.2 # phpize # ./configure # make # make install 

With the extension installed, all you need to do to activate the new extension is add the following line to your php.ini file and restart the web server.

 [sqlite] extension="sqlite.so" 

Alternatively you can also load the SQLite extension dynamically in each script that requires it with the dl() function.

 dl("sqlite.so"); 

There should be no need to specify the full path to sqlite.so; the shared object file will be installed to the extension_dir value in php.ini.

Configuring PHP for Windows

The Windows DLL version of the SQLite extension is bundled with PHP5, and for earlier versions it can be downloaded from http://snaps.php.net/win32/PECL_STABLE/php_sqlite.dll.

Save this file to the PHP extensions directoryusually C:\php\extensionsand then activate the module by adding the following lines to your php.ini file and restarting Apache.

 [sqlite] extension=php_sqlite.dll 

Checking for SQLite Support

If the SQLite extension is loaded, you can see some information about the module in the relevant section of the page generated by the phpinfo() function. Figure 5.1 shows the typical output when SQLite support is present.

Figure 5.1. Output of phpinfo() showing SQLite support in PHP.


In the command-line PHP, you can check which modules are loaded with the m switch, for instance:

 $ php -m | grep sqlite sqlite 

You can also check whether the extension is loaded programmatically using the extension_loaded() function, as demonstrated in Listing 5.1. This listing will attempt to load the extension if it is not already present, and exit if the attempt fails.

Listing 5.1. Check to Find Whether SQLite Extension is Available to PHP
 <?php if (!extension_loaded("sqlite")) {   if (!dl("sqlite.so")) {     echo "The SQLite extension could not be loaded.  Exiting.";     exit;   } } // Continue with SQLite-dependent code here ?> 

Getting Information About the SQLite Extension

When you are sure the SQLite extension is available in your version of PHP, there are two functions available to return information about the version and encoding of the attached library.

These are the values we saw in Figure 5.1 in the output of phpinfo(). The functions allow you to use the respective values programmatically if you want to perform conditional actions based on the library version, for instance.

A call to sqlite_libversion() requires no arguments and returns a text string containing the version number of the linked library. On the system that the screenshot in Figure 5.1 was taken on, this would return the string 2.8.11. The function sqlite_libencoding() would return iso8559 on the same system.

By default PHP builds libsqlite with ISO-8859-1 encoding. In fact, this is not strictly ISO-8859-1it operates according to your system local settings. The alternative, UTF-8, is not fully supported by PHP at the time of writing and may produce unexpected problems.



    SQLite
    SQLite
    ISBN: 067232685X
    EAN: 2147483647
    Year: 2004
    Pages: 118
    Authors: Chris Newman

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net