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 PC World newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Matthew Stivala

HP OfficeJet 250 Mobile Printer

The HP OfficeJet 250 Mobile Printer is a great device that fits perfectly into my fast paced and mobile lifestyle. My first impression of the printer itself was how incredibly compact and sleek the device was.

Armand Abogado

HP OfficeJet 250 Mobile Printer

Wireless printing from my iPhone was also a handy feature, the whole experience was quick and seamless with no setup requirements - accessed through the default iOS printing menu options.

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?