Caching Result Sets in PHP: Object Interaction Within a Caching System - The third link in the caching process: a quick look at the “Result” class (
Page 4 of 5 )
Very often it is much better to delegate the responsibility for manipulating database result sets to a separated class, rather that assigning these tasks to the same class that connects to MySQL. That’s the primary reason for the existence of the “Result” class. However, don’t be concerned about it. It only takes care of fetching data, counting table rows and other result-related operations.
Here is the proper list for the “Result” class:
class Result{
var $mysql; // instance of MySQL object
var $result; // result set
// constructor
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 data');
}
}
}
Although the methods belonging to the above class might be appended to the MySQL class, it’s much more efficient having a separated structure that accomplishes all of the row-related operations. So, as you can see, this class allows us to fetch rows, count result rows, and even get the ID from the last inserted record.
Of course, more methods may be eventually added, useful for improving the functionality within the class. But, as I said before, we’ll keep it simple.
By this point, we’ve covered in detail each class that interacts with “Cache.” The next step is putting all of the pieces together and demonstrating a practical example.