The MySQLResultSet Class
Not surprisingly, the MySQLResultSet class (shown in Listing 9-1) has more data members and methods than the MySQLConnect class. However, in many ways, it's a much simpler class and requires much less explanation. To get an overview of this class, find all its data members and methods listed here:
//data members private $strSQL private $databasename private $connection private $result // public methods public function _construct($strSQL, $databasename, $connection)
public function _destruct()
- return current record public function getRow()
- accessor method for returning database name public function getDatabaseName()
public function getNumberColumns()
public function getNumberRows()
- get id of most recently inserted record public function getInsertId()
- find total number without a LIMIT clause public function getUnlimitedNumberRows()
public function getFieldNames()
public function findVersionNumber()
- private methods
- make sure the sql is a SELECT
private function checkForSelect()
- close result set and unset private function close()
- version specific count methods private function countVersionFour()
private function countVersionThree($tempsql, $end)
Listing 9-1: The MySQLResultSet class
You've already seen the constructor for this class, but a few general comments are in order before looking at any of the methods in more detail.
One notable absence from the list of methods is a method equivalent to the getArraySlice method of the DirectoryItems class. You could have created something equivalent by selecting all the required records and then using the built-in function mysql_data_seek to reposition the record pointer as necessary, but the price to pay for this relatively easy implementation would be poor performance. Imagine paging through 1,000 records 10 records at a time and for each page, bringing over all 1,000 records. The more scalable solution is to restrict the number of records selected by using a LIMIT clause in the SQL that creates the result set.
However, in order for your page navigator to function, you also need to know the total number of records without a LIMIT clause. With MySQL versions 4.0 and higher, there is an easy way of doing this using SQL_CALC_FOUND_ROWS, followed by a call to the FOUND_ROWS function. For MySQL version 3, you can use the COUNT function without a LIMIT clause.
This is a fairly easy process to automate, so to make things easier on yourselves and your client programmers, you create the getUnlimitedNumberRows method. Briefly, the getUnlimitedNumberRows method confirms that the query is a SELECT, determines the MySQL version number, and discovers the total number of records that would be returned without a LIMIT clause by calling the private method countVersionThree or countVersionFour.
Most of the remaining methods are simply wrapper methods for existing MySQL functions, or they make use of these functions to perform fairly straightforward tasks. You won't actually be using some of these methods— getNumberColumns, for instance—but they give you an idea of how this class could be expanded.
This isn't the last you'll see of the MySQLResultSet class. We'll return to it again in Chapter 10 because it provides an ideal opportunity for further exploring OO programming. Right now though, your primary concern is to see how it functions with the PageNavigator class.
Post a comment