However, the point to stress in this case is that the prior class resides in a separate file, called “mysql.php,” which should be included manually into this sample application via a "require_once()" PHP function. Also, to make the application work as expected, I’m going to define an additional class, called “Result,” which will reside in a different file called “result.php.” It will look like this: class Result{ private $mysql; private $result; // constructor public function __construct($mysql,$result){ $this->mysql=$mysql; $this->result=$result; } // public 'fetch()' method public function fetch(){ return mysql_fetch_array($this->result,MYSQL_ASSOC); } // public 'count()' method public function count(){ if(!$rows=mysql_num_rows($this->result)){ throw new Exception('Error counting rows'); } return $rows; } // public 'get_insertId()' method public function getInsertId(){ if(!$insId=mysql_insert_id($this->mysql->connId)){ throw new Exception('Error getting insert ID'); } return $insId; } // public 'seek()' method public function seek($row){ if(!int($row)&&$row<0){ throw new Exception('Invalid row parameter'); } if(!$row=mysql_data_seek($this->mysql->connId,$row)){ throw new Exception('Error seeking row'); } return $row; } // public 'getAffectedRows()' method public function getAffectedRows(){ if(!$rows=mysql_affected_rows($this->mysql->connId)){ throw new Exception('Error counting affected rows'); } return $rows; } } As you can see, the above “Result” class comes in handy for processing MySQL result-sets in all sorts of clever ways. And it’s capable of doing some interesting things, like fetching and counting the rows returned in a data set, determining the ID of a particular insert operation, etc. It's nothing too complex, actually. Besides, as I said before, this class is stored in a “result.php” file, which should be included in this sample database-driven application via a “require_once()” function, as you’ve possibly done hundreds of times before. So, having showed you the respective definitions of the two previous MySQL handling classes, I want to fetch a few database records from a basic “Users” table and display them on the browser. The following script should do the job quite decently: try{ // include required classes require_once 'mysql.php'; require_once 'result.php'; // connect to MySQL $db=new MySQL(array // fetch users from database table $result=$db->query('SELECT * FROM users ORDER BY id'); // display user data while($row=$result->fetch()){ echo 'Id: '.$row['id'].' First Name: '.$row['firstname'].' Last Name: '.$row } } catch(Exception $e){ echo $e->getMessage(); exit(); } In this case, the above script will display some basic data about a few fictional users, including their first and last names, by using the pair of MySQL processing classes that you learned before. Nonetheless, the most important thing to highlight here is that the classes are loaded “manually” by using the popular “require_once()” PHP native function. This is a process that should be familiar to you. So far, I've recreated a hypothetical situation where the classes required by a certain database-driven application are included manually into client code via a couple of “require_once()” functions. Now that you've seen how this simple process is achieved, it’s time to take a close look at the handy “__autoload()” PHP 5 magic function. As you’ll see in a few moments, it will allow you to load the previous classes “automatically" and logically without having to use any “require_once()” statements. To learn how this handy function will be used in the context of the prior database-driven application, please click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|