Flat File Database Demo 4

Now with added search function!

16th January 2009 · Last updated: 5th October 2016


Doug F emailed me to ask if it was possible to add a search function to my flat-file database script. It is! In fact I had already written a basic search function before but for different code. So I added new code from scratch that would work with the second flat-file demo here.

You can now search for a word in the database and it will show each row that contains it. I even added a highlight to the words that match your search!

A simple form is used to enter the search word on the first page. This is passed to a second page that looks for the word in each field of the database. If the word is found then the script will also output the full row from the database into a table. If the word isn't found then a simple error message is shown.


Update 21st January 2009: Improved the code so part words can be searched for. It will also pick up words of any case (upper or lower, or mixed).

Update 20th April 2010: It now displays the number of searches found. Thanks to Frank Höfken for the inspiration!

Comments (4)

Comments are locked on this topic. Thanks to everyone who posted a comment.

  1. Amir:
    how we can delete specific line from text file

    Sent in by email on 24th February 2009
  2. Joe:
    This is just what I was looking for

    Sent in by email on 6th August 2009
  3. PGR:
    Hey there,

    I've found this database code extremely helpful! I am using a similar and somewhat customized code of the one in Demo 1, and I would just like to ask a question about it.

    How would I search the database, and only display lines that begin with a certain number (a serial number)?

    Thanks a lot! This is great!

    Again, thanks for this amazing script! I use it all the time!!

    Sent in by email on 17th September 2009
  4. Chris Hester:
    Just loop through the database and check the first field (by splitting the line as in the demo). Then just check if it is the serial number you want. If so, split the line again to get the whole of the fields in the line, then echo the contents of that line as required (eg: using table tags). Something like this should work from within the main loop:

    list ($serialnumber) = split ('\|', $line);
    if ($serialnumber == $searchstring) {
    list ($serialnumber, $product, $description, $price, $date) = split ('\|', $line);
    echo <<<HTML

    17th September 2009