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($result, 0); 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.
Next: Creating a Search Application >>
More Database Articles Articles
More By Matt Wade