About the Author

Chris Shiflett

Hi, I’m Chris: web craftsman, community leader, husband, father, and partner at Fictive Kin.

PHP Advent Calendar Day 20

Today's entry, provided by Adam Trachtenberg, is entitled User-Defined Functions in SQLite.

Adam Trachtenberg

Adam Trachtenberg
Adam Trachtenberg is the Senior Manager of Platform Evangelism and Disruptive Innovation at eBay, where he preaches the gospel of the eBay platform to developers and businessmen around the globe. He's the author of Upgrading to PHP 5 and coauthor of PHP Cookbook, both published by O'Reilly Media. Adam lives in San Francisco, California, which he wishes was closer to the office.
San Francisco, California

SQLite is a database that's bundled with PHP 5. Unlike most other databases, SQLite is not a separate application; it's an extension that reads from and writes to regular files.

Although the name SQLite hints at a less than full-featured product, besides the usual INSERTs and SELECTs, SQLite also boasts transactions, subselects, and triggers.

My favorite SQLite feature is the one that allows you to write your own SQL functions. Because, in addition to all the built-in SQL functions, such as lower() and upper(), you can extend SQLite to include your own functions that you write in PHP.

These are known as user-defined functions, or UDFs for short. With a UDF, you embed logic into SQLite and avoid doing it yourself in PHP. Thus, you can take advantage of all of the features inherent in a database, such as sorting and finding distinct entries.

UDFs are good for chopping up strings so you can perform nonstandard collations and groupings. For example, you want to sort through a list of URLs, maybe from a referrer log file, and create a list of unique hostnames sorted alphabetically. So, http://example.com/directory/index.html and http://example.com/page.html would both map to one entry: http://example.com/.

To do this in PHP, you need to retrieve all the URLs, process them inside your script, and then sort them. Plus, somewhere in all that, you need to remove the duplicates. If it weren't for that pesky URL-conversion process, this could all be done in SQL using DISTINCT and ORDER BY.

With a UDF like the following, you foist all that hard work back onto SQLite where it belongs:

// CREATE table and INSERT URLs.
$db = sqlite_open('/www/support/log.db');
$sql = 'CREATE TABLE access_log(url);';
$urls = array('http://example.com/directory/index.html', 
foreach ($urls as $url) {              
    $sql .= "INSERT
             INTO   access_log
             VALUES ('$url');";
sqlite_query($db, $sql);
// UDF Written in PHP
function url2host($url) {
    $parts = parse_url($url);
    return "{$parts['scheme']}://{$parts['host']}/";
// Map the PHP function url2host() to the SQL function host(),
// and indicate that host() will take 1 argument.
sqlite_create_function($db, 'host', 'url2host', 1);
$r = sqlite_query($db, 'SELECT
                        DISTINCT host(lower(url))
                        AS       clean_host 
                        FROM     access_log
                        ORDER BY clean_host;');
// Loop through results.
while ($row = sqlite_fetch_array($r)) {
    echo "{$row['clean_host']}\n";

As expected, this outputs the following:


To use a UDF, you first write a regular function in PHP. The function's arguments are what you want to pass in during the SELECT, and the function should return a single value. The url2host() function takes a URL, calls the built-in PHP function parse_url() to break the URL into its component parts, and returns a string containing the scheme (http) and the host. So, http://example.com/directory/index.html gets broken apart into many pieces. http is stored in $parts['scheme'], and example.com goes in $parts['host']. This creates a return value of http://example.com/.

The next step is to register url2host() with SQLite using sqlite_create_function(). This function takes four arguments:

  1. The Database Handle

  2. The SQLite Function Name of Your Choice

  3. The Function's Name in PHP

  4. The Number of Expected Arguments

The last argument is optional, but if you know for certain that your function only accepts a specific number of arguments, providing this information helps SQLite optimize things behind the scenes. In this example, the SQL function is host(), while the PHP function is url2host(). These names can be the same; they're different here to make the distinction between them clear.

Now you can use host() inside any SQL query that use the same database connection. The SQL above SELECTs host(lower(url)) AS clean_host. This takes the URL stored in the url column, converts it to lowercase, and calls the UDF host().

The function is not permanently registered with the database, and it goes away when you close the database. If you want to use it when you reopen the database, you must reregister it. Also, the function is only registered for that database; if you open up a new database using sqlite_connect(), you need to call sqlite_create_function() again.

The returned string is then named AS clean_host; this lets you refer to the results later on in the SQL query as well as access the value in PHP using that name. Since you're still in SQLite, you can take advantage of this to sort the list using ORDER BY clean_host. This sorts the results in alphabetical order.

There are plenty of other things you can do with UDFs, but hopefully this is enough to pique your interest and get you started.

About this post

PHP Advent Calendar Day 20 was posted on Thu, 20 Dec 2007. If you liked it, follow me on Twitter or share:


1.Elizabeth Smith said:

Something you didn't mention is that there's a magic "php" function registered for you, so you can use any php function inside sqlite without registering it beforehand.

$rows = sqlite_array_query($dbhandle, "SELECT php('md5', filename) from files");

I love that trick ;)

Thu, 20 Dec 2007 at 20:33:22 GMT Link

Hello! What’s your name?

Want to comment? Please connect with Twitter to join the discussion.