As you just saw in the preceding segment, the “query()” method that belongs to the “MySQLiWrapper” class comprises a new object of type “MySQLi_ResultWrapper.” As its name suggests, this object is tasked with processing result sets; it also inherits functionality from the native MySQLi_Result PHP 5 class. You may be asking yourself what the originating class of this object looks like. Well, to answer that question, pay attention to the following code sample, which shows the definition of that class: class MySQLi_ResultWrapper extends MySQLi_Result implements Iterator, Countable { private $_pointer = 0;
// fetch row as an object public function fetchObject() { if (!$row = $this->fetch_object()) { return NULL; } return $row; }
// fetch row as an associative array public function fetchAssocArray() { if (!$row = $this->fetch_assoc()) { return NULL; } return $row; }
// fetch row as an enumerated array public function fetchNumArray() { if (!$row = $this->fetch_row()) { return NULL; } return $row; }
// fetch all rows public function fetchAll($type = MYSQLI_ASSOC) { if ($type !== MYSQLI_ASSOC AND $type !== MYSQLI_NUM AND $type !== MYSQLI_BOTH) { $type = MYSQLI_ASSOC; } if (!$rows = $this->fetch_all($type)) { return NULL; } return $rows; }
// get definition information on fields public function fetchFieldsInfo() { if (!$fieldsInfo = $this->fetch_fields()) { throw new Exception('No information available for table fields.'); } return $fieldsInfo; }
// get definition information on next field public function fetchFieldInfo() { if (!$fieldInfo = $this->fetch_field()) { throw new Exception('No information available for current table field.'); } return $fieldInfo; }
// move pointer in result set to specified offset public function movePointer($offset) { $offset = abs((int)$offset); $limit = $this->num_rows - 1; if ($limit <= 0 OR $offset > $limit) { return FALSE; } unset($limit); return $this->data_seek($offset); }
// count rows in result set (implementation required by 'count()' method in Countable interface) public function count() { return $this->num_rows; }
// reset result set pointer (implementation required by 'rewind()' method in Iterator interface) public function rewind() { $this->_pointer = 0; $this->movePointer($this->_pointer); return $this; }
// get current row set in result set (implementation required by 'current()' method in Iterator interface) public function current() { if (!$this->valid()) { throw new Exception('Unable to retrieve current row.'); } $this->movePointer($this->_pointer); return $this->fetchObject(); }
// get current result set pointer (implementation required by 'key()' method in Iterator interface) public function key() { return $this->_pointer; }
// move forward result set pointer (implementation required by 'next()' method in Iterator interface) public function next() { ++$this->_pointer; $this->movePointer($this->_pointer); return $this; }
// determine if result set pointer is valid or not (implementation required by 'valid()' method in Iterator interface) public function valid() { return $this->_pointer < $this->num_rows; }
// free up result set public function __destruct() { $this->close(); } } Despite the rather lengthy definition of the above “MySQLi_ResultWrapper” class, its underlying logic is very easy to follow. Most of its methods are inherited from the “Iterator” and “Countable” interfaces packaged with the SPL library. Every object spawned from this class will be able to iterate over a given result set by using a simple “foreach” construct. In addition, the class includes a few additional methods that fetch rows from the queried table in different flavors, and retrieve information about its comprising fields as well. Having discussed how the previous “MySQLi_ResultWrapper” does its business, I'm now going to code a script that shows how to use the MySQL driver in a useful fashion. This will be done in the final part of this tutorial. Therefore, to see how this script will be built, read the following segment. It’s only one click away.
blog comments powered by Disqus |
|
|
|
|
|
|
|