Search Code
  Home arrow Search Code arrow Table Searcher
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
SEARCH CODE

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

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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

     

    IBM® developerWorks developerWorks - FREE Tools!


    Be the first to hear about i5/OS V6R1!

    Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br />
    FREE! Go There Now!


    NEW! Driving Business Success with Rational Process Library

    Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance.
    FREE! Go There Now!


    NEW! Cook up Web sites fast with CakePHP, Part 4: Use CakePHP&apos;s Session and Request Handler components

    CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP.
    FREE! Go There Now!


    NEW! Develop Systems Software Assets with IBM Rational Asset Manager

    Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager.
    FREE! Go There Now!


    NEW! Hello World: Monitor a simple business process using WebSphere Business Monitor V6.0.2

    This tutorial shows new users of IBM WebSphere Business Monitor Version 6.0.2 how to perform the "Hello World" equivalent for monitoring business process applications. It is intended to help you get familiar with the capabilities of the product.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Manual Tester V7.0.1

    Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually.
    FREE! Go There Now!


    NEW! Using IBM Rational Developer for System z and IBM Rational ClearCase together to manage application development

    Whether you are creating new applications or modifying existing ones, managing integration of new components with traditional z/OS elements is a critical part of building and deploying modern applications. Listen to this webcast to see how IBM can help you optimize your development process using an IDE like Rational Developer for System z that integrates with management tools, such as ClearCase to manage your application development on mainframes.
    FREE! Go There Now!


    NEW! Webcast: Calling All Testers! Find Application Vulnerabilities Early in the Development Process Where they are Easier to Fix and Less Risky to your Business

    In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information.
    FREE! Go There Now!


    NEW! Webcast: Eclipse: Empowering the universal platform

    The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now.
    FREE! Go There Now!


    NEW! Webcast: WebSphere Process Server

    WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    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-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT