Search Code

  Home arrow Search Code arrow Table Searcher
SEARCH CODE

Table Searcher
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2003-03-08

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    A simple function to search through a table in a MySQL database. Just fill in the variables when you call the function.           Try it out with ?userquery=i  

    By : bigphish

    <?php
    function searchtable($database,$tablename,$userquery,$host,$user,$pass){
    $link = mysql_connect($host, $user, $pass) or die("Could not connect: " . mysql_error());
    $db = mysql_select_db($database, $link) or die(mysql_error());
    $fields = mysql_list_fields($database, $tablename, $link);
    $cols = mysql_num_fields($fields);
    $content = "";
    for ($i = 0; $i < $cols; $i++) {
    $allfields[] = mysql_field_name($fields, $i);
    }
    foreach ($allfields as $myfield) {
    $result = mysql_query("SELECT * FROM $tablename WHERE $myfield like '%$userquery%' ");
    if (mysql_num_rows($result) > 0){
    $content .= "<h3>search <i>$database</i> for <i>$userquery</i>, found match(es) in <i>$myfield</i>: </h3>\n";
    $content .= "<table border=1 align=\"center\">\n\t<tr>\n";
    for ($i = 0; $i < $cols; $i++) {
    $content .= "\t\t<th";
    if ($myfield == mysql_field_name($fields, $i)){
    $content .= " bgcolor=\"orange\" ";
    }
    $content .= ">" . mysql_field_name($fields, $i) . "</th>\n";
    }
    $content .= "\t</tr>\n";
    $myrow = mysql_fetch_array($result);
    do {
    $content .= "\t<tr>\n";
    for ($i = 0; $i < $cols; $i++){
    $content .= "\t\t<td";
    if (preg_match("/$userquery/i", $myrow[$i])){
    $mysplit = preg_split("/($userquery)/i", $myrow[$i], -1,PREG_SPLIT_DELIM_CAPTURE);
    $content .= " bgcolor=\"orange\">";
    for ($si=0;$si<count($mysplit);$si++){
    if (preg_match("/$userquery/i", $mysplit[$si])){
    $content .= "<font color=\"green\"><b>$mysplit[$si]</b></font>";
    }else{
    $content .= "$mysplit[$si]";
    }
    }
    } else {
    $content .= ">$myrow[$i]";
    }
    $content .= "&nbsp;</td>\n";
    }
    $content .= "\t</tr>\n";
    } while ($myrow = mysql_fetch_array($result));
    $content .= "</table>\n";
    }
    }
    mysql_close($link);
    return $content;
    }

    $page_content = searchtable(yourdb,your table,$userquery,yourhost,youruser,yourpass);
    echo $page_content;


    ?>


    //Database used as demo on my site

    #
    # Table structure for table `thistest`
    #

    CREATE TABLE thistest (
    firstname varchar(20) NOT NULL default '',
    lastname varchar(20) NOT NULL default '',
    phone varchar(20) NOT NULL default '',
    city varchar(20) NOT NULL default ''
    ) TYPE=MyISAM;

    #
    # Dumping data for table `thistest`
    #

    INSERT INTO thistest VALUES ('Joe', 'Smith', '(888) 555-1234', 'Mexico City');
    INSERT INTO thistest VALUES ('Jane', 'Jiggles', '(888) 555-2345', 'Las Vegas');
    INSERT INTO thistest VALUES ('Waldo', 'Wiggum', '(888) 555-3355', 'Tuktoyuktuk');
    INSERT INTO thistest VALUES ('Big', 'Phish', '(888) 555-5555', 'San Diego');
    INSERT INTO thistest VALUES ('Captian', 'Crunch', '(888) 888-8888', 'Mississippi');


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Search Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    SEARCH CODE ARTICLES

    - PHP IP Blocking
    - Search engine with ranking feature
    - Ajax access to Google API
    - Table Searcher
    - Search_It
    - FTP SEARCH
    - Search and Replace class
    - Simple Search on mysql database
    - Search Your Database
    - search_v1.1
    - Database Searching Class


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap