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  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
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: 3 stars3 stars3 stars3 stars3 stars / 4
    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!


    NEW! Best Practices: The Integrated Project and Portfolio Management Platform.

    Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan.
    FREE! Go There Now!


    NEW! A Layered approach to delivering security-rich Web applications

    As businesses grow increasingly dependent upon Web applications to provide services to customers, employees and partners, these complex applications become more difficult to secure. Although traditional security solutions protect Internet infrastructure layers, they do not guard against HTTP and HTML attacks. Many organizations that conduct security testing still deploy applications that allow attackers to manipulate their logic and wreak havoc on their business. To mitigate this risk, development and delivery teams must address Web application security throughout the lifecycle, addressing the many layers detailed in this paper.
    FREE! Go There Now!


    NEW! Discovering the value of 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!


    NEW! Download IBM Rational Developer for System z

    Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects.
    FREE! Go There Now!


    NEW! Harnessing the power of SQL and Java for high performance data access

    Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services.
    FREE! Go There Now!


    NEW! Rational 'Talks to You' Teleconference Series

    This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today!
    FREE! Go There Now!


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

    Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads.
    FREE! Go There Now!


    NEW! Understanding Web application security challenges

    As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security.
    FREE! Go There Now!


    NEW! Whitepaper: Delivering SOA solutions: service lifecycle management

    The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable.
    FREE! Go There Now!


    Refresh! IBM Rational Systems Development Solution eKit

    With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential.
    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-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT