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.

Recommended

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World

Comments

Comments are now closed.

Most Popular Reviews

Follow Us

Best Deals on GoodGearGuide

Shopping.com

Latest News Articles

Resources

GGG Evaluation Team

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell

LIFEBOOK UH574

The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi

STYLISTIC Q702

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott

STYLISTIC Q702

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?