We’ve already seen that the method “performQuery()” returns a new instance of a “Result” object. Therefore, there must be a class that defines the blueprints for it. Fortunately, this class is quite simple to read, and its definition is as follows: class Result { var $mysql; // instance of MySQLConnector object var $result; // result set function Result(&$mysql,$result){ $this->mysql=&$mysql; $this->result=$result; } // fetch row function fetchRow(){ return mysql_fetch_array($this->result,MYSQL_ASSOC); } // count rows function countRows(){ if(!$rows=mysql_num_rows($this->result)){ $this->mysql->isError('Error counting rows'); } return $rows; } // count affected rows function countAffectedRows(){ if(!$rows=mysql_affected_rows($this->mysql->conId)){ $this->mysql->isError('Error counting affected rows'); } return $rows; } // get ID from last inserted row function getInsertID(){ if(!$id=mysql_insert_id($this->mysql->conId)){ $this->mysql->isError('Error getting ID'); } return $id; } // seek row function seekRow($row=0){ if(!mysql_data_seek($this->result,$row)){ $this->mysql->isError('Error seeking row'); } } } Our brand new “Result” class behaves in a simple manner, and its constructor takes two parameters (remember how it was created within the “MySQLConnector” class). The first parameter &$mysql, is a reference of a “MySQLConnector” object, in order to provide database connectivity, and the second, $result is the result set obtained after performing the query. Once passed, they’re assigned as class data members. The rest of the methods are pretty easy to follow, as you’ll probably agree. The “fetchRow()” method is used to return a row from the result set, as we’ve probably seen hundreds of times. To provide the class with the ability to count rows, I’ve defined the “countRows()” method, which simply retrieves the number of rows returned by the query. This might be useful for a paging class or other system that needs to find out the total number of rows. Okay, now hold on your breath a little longer until we’ve seen the rest of the class methods. The next remaining methods are “countAffectedRows()”, handy for finding out how many rows were affected by an INSERT, UPDATE or DELETE SQL statement; “getInsertID()”, which retrieves the ID of an AUTO_INCREMENT field, after an insertion operation; and finally “seekRow(), that places the pointer at a specified position within the result set. As you might guess, I’ve added to the class the most common operations associated with MySQL. However, you might add to it your own methods to extend its functionality. Notice that each method handles an error condition by calling directly the “isError()’ method, provided by “MySQLConnector” class, like this:
As you can appreciate, “isError()” is invoked by all of the methods, providing a centralized mechanism to handle errors. One possible improvement would be writing a method that degrades the application gracefully when an error occurs, instead of displaying ugly messages. At this time, we’ve finished defining our “Result” class, so we’re ready to implement the classes. Just keep reading to find out how they’re used in a PHP application.
blog comments powered by Disqus |
|
|
|
|
|
|
|