Database Articles
  Home arrow Database Articles arrow Page 7 - Creating a Search Application
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? 
DATABASE ARTICLES

Creating a Search Application
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 14
    2003-07-15

    Table of Contents:
  • Creating a Search Application
  • Database Usage
  • Creating a Search Application
  • Searching
  • Conclusion

  • 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


    Creating a Search Application -


    (Page 7 of 29 )

    Now that we have the basics of working with databases covered, let's build a PHP class to handle our database needs for our search application. So that you may have a better understanding of what a class is, we will define it as a "container for data and functions that work with that data."

    Why would we want to write a database class? Well, it can benefit us in a couple of ways. One, it makes other code we write easier to read and follow. By taking care of all the database needs in a class, we can cut down on the amount database code in our search routines. This will allow us to more closely follow what is happening in the search application, without having all the database code getting in the way. Secondly, a database class can be expanded in the future. It gives you a single place to update the database functionality. For an example, if you later wanted to port the search application to another type of database server, you would only need to update the database class. Without a class, you would need to modify all the database code throughout the search application.

    Now that we have established why we want to create a database class, let's get started. The name of our database class will be DB_Class. Let's take a look at the functionality the class will provide, and then we will present the class itself.

  • Constructor - The constructor of the class bears the same name as the class and is called when a new instance of the class is created. In our constructor, we will make the connection to the database server and select the proper database. The constructor will accept a database name, username, and password as parameters.
  • query Function - This function will be named query and we will be used to run any SQL statement given to it. It will return a resource id. This function will be used by other functions of the class and it will also be called from outside the class.
  • fetch Function - This function, named fetch, will accept a string containing a SQL statement and will return an array containing the results from the query.
  • getone Function - The getone function will return the value of the first column on the first row return by the SQL statement passed to it. If the SQL statement does not yield any results, it will return FALSE.

    We will now take a look at the class itself. We will not spend time examining the class in detail as it is made up of functions and concepts we have already covered.

    <?php
    class DB_Class {

        var 
    $db;

        function 
    DB_Class($dbname$username$password) {
            
    $this->db mysql_connect ('localhost'$username$password)
                or die (
    "Unable to connect to Database Server");

            
    mysql_select_db ($dbname$this->db)
                or die (
    "Could not select database");
        }

        function 
    query($sql) {
            
    $result mysql_query ($sql$this->db)
                or die (
    "Invalid query: " mysql_error());
            return 
    $result;
        }

        function 
    fetch($sql) {
            
    $data = array();
            
    $result $this->query($sql);
            while(
    $row mysql_fetch_assoc($result)) {
                
    $data[] = $row;
            }
            return 
    $data;
        }

        function 
    getone($sql) {
            
    $result $this->query($sql);
            if(
    mysql_num_rows($result) == 0)
                
    $value FALSE;
            else
                
    $value mysql_result($result0);
            return 
    $value;
        }

    }
    ?>

    Example use of the database class:

    <?php
    $db 
    = new DB_Class('test''username''password');
    $data $db->fetch("SELECT * FROM introduction");
    ?>

    This concludes our brief introduction to using databases. We strongly recommend that you pick up a book exclusively on the subject of PHP and databases as there is much more to learn on the subject.

    More Database Articles Articles
    More By Matt Wade


       · 
       · I've copy and paste the five files regarded in this tutorial, and created the db,...
       · Hello,


    'id' INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,

    I'm...
       · THANKS - MATTVery clear explained tutorial.Wonderful search, I am...
       · My phpadmin had no issues with the table creation, but can't get the harvest script...
       · It's an ok search tutorial, but too many errors.[QUOTE]Warning: array_walk()...
       · Where to post for help on this tutorial?CheersWest
       · great tutorial and very inspiring column in your conclusion.
     

  • DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT