Search multiple fields with PHP

This column will look at modifying 'Address book in PHP revisited's' AB2 code to allow searching of multiple data fields.

Python vs. PHP: Choosing your next project's language

Working with Indices

AB2 is designed to store address data in relation to the last name of the person's address. In database talk, AB2 is 'building an index' over the last names in the data received, since the last name is used as the 'key' to the database. To enhance the functionality of AB2, we will also build an index over the first name. This is referred to as 'building a secondary index' on the data set. Together, the two are referred to as indices.

There are several ways we can do this:

1) Build two databases, the first with the last name as a key, the second with the first name as the key. 2) Assign each new entry a unique identifier. Build two databases, the first relating last names and first names to identifiers by storing the last name and first name as the key and the identifier as the value, and the second relating identifiers to address book data.

Of these two, the first option is the simplest to implement so we will take this as a starting point. Those wanting to hone their PHP and database skills should attempt to implement the second.

Here, then, is an implementation of the data submission system for the first option:

<?
$DBLN="/tmp/address-ln.db";
$DBFN="/tmp/address-fn.db";
if(isset($submit) && (strcmp($submit,"Add") == 0)) {
$lnid = mkdb($DBLN);
$fnid = mkdb($DBFN);
$data = "F=".rawurlencode($fn)."&L=".rawurlencode($ln)."&" /* etc... */
if(dba_insert($ln,$data,$lnid)) {
echo "Data successfully stored in $DBLN<br />";
dba_close($lnid);
} else {
echo "Could not store data in $DBLN!";
dba_close($lnid);
exit();
}
if(dba_insert($fn,$data,$fnid)) {
echo "Data successfully stored in $DBFN<br />";
dba_close($fnid);
} else {
echo "Could not store data in $DBFN!";
dba_close($fnid);
exit();
}
}
?>

This code is quite straightforward. Instead of submitting data to the database '/tmp/address.db' as AB2 did, AB2.1 inserts the data into two databases: '/tmp/address-fn.db' and '/tmp/address-ln.db', which builds an index across the last name.

Now for the search:

<?
$DBLN="/tmp/address-ln.db";
$DBFN="/tmp/address-fn.db";
if(isset($submit) && (strcmp($submit,"Search") == 0)) {
if(!file_exists($DBFN) || !file_exists($DBLN)) {
/* database has not been created yet */
exit("No entries to search");
} /* search by ln first */
if(!($lnid = dba_open($DBLN,"r","db3"))) {
exit("Could not open $db\n");
}
if(($str = dba_fetch($query,$lnid))) {
/* found the query */
parse_str($str);
?>
Name: <? echo rawurldecode($F); ?> <? echo rawurldecode($L); ?>
<?
/* etc... */
$lnhash = md5($str);
}
dba_close($lnid);

if(!($fnid = dba_open($DBFN,"r","db3"))) { exit("Could not open $DBFN\n"); } if(($str = dba_fetch($query,$fnid))) { /* found the query,same as the last? */ if(strcmp(md5($str),$lnhash) != 0) { parse_str($str); ?> Name: <? echo rawurldecode($F); ?> <? echo rawurldecode($L); ?> <? /* etc... */ } } dba_close($fnid); } ?>

This code is a little trickier. Like AB2, the search script here first attempts to search for the last name, this time in the database '/tmp/address-ln.db'. The script then attempts to search for $query as a first name, by querying '/tmp/address-fn.db'. Note that in the second search, the script attempts to avoid duplicate output (which could occur if a submit to the address book had identical first and last name fields). It does this with the help of the message-digest algorithm, MD5, which builds a unique representation of any given length of text in the form of a 16-character string. (More information can be found at www.faqs.org/rfcs/rfc1321.html.)By building MD5 message-digests of the data retrieved from a search of each database, the script can determine very easily whether or not the data output for the last name search is identical to the data retrieved in the first name search. If the message-digests are not equal, any results from the second search are output.

Shortcomings of AB2.1

The implementation of secondary indices in AB2.1 still requires that more code be written for every field, which needs to be indexed. In all fairness, this is a flaw in PHP's Database Abstraction layer, which does not allow the user access to Berkeley DB's native support of secondary indices.

There are other problems, too: the Database Abstraction layer does not properly support multiple database users at any given time. In the world of databases, this functionality is called 'Multi-version Concurrency Control'. Next month, we will begin our first foray into the world of Relation Databases by focusing on PHP and PostgreSQL.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

More about ABHewlett-Packard AustraliaHP

Show Comments
[]