Implements: ArrayAccess | SeekableIterator | Traversable | Iterator | Countable
MySQL database result. See Results for usage and examples.
Class declared in MODPATH/database/classes/Database/MySQL/Result.php on line 3.
$_as_objectNULL
$_current_rowinteger 0
$_internal_rowinteger 0
$_object_paramsNULL
$_queryNULL
$_resultNULL
$_total_rowsinteger 0
Sets the total number of rows and stores the result locally.
mixed
$result
required - Query resultstring
$sql
required - SQL querymixed
$as_object
= bool FALSE - $as_objectarray
$params
= NULL - $paramsvoid
public function __construct($result, $sql, $as_object = FALSE, array $params = NULL)
{
parent::__construct($result, $sql, $as_object, $params);
// Find the number of rows in the result
$this->_total_rows = mysql_num_rows($result);
}
Result destruction cleans up all open result sets.
void
public function __destruct()
{
if (is_resource($this->_result))
{
mysql_free_result($this->_result);
}
}
public function current()
{
if ($this->_current_row !== $this->_internal_row AND ! $this->seek($this->_current_row))
return NULL;
// Increment internal row for optimization assuming rows are fetched in order
$this->_internal_row++;
// FIXME mysql_fetch_object has been deprecated as of php 5.5!
// Please use mysqli_fetch_object or PDOStatement::fetch(PDO::FETCH_OBJ) instead.
if ($this->_as_object === TRUE)
{
// Return an stdClass
return mysql_fetch_object($this->_result);
}
elseif (is_string($this->_as_object))
{
/* The second and third argument for mysql_fetch_object are optional, but do
* not have default values defined. Passing _object_params with a non-array value results
* in undefined behavior that varies by PHP version. For example, if NULL is supplied on
* PHP 5.3, the resulting behavior is identical to calling with array(), which results in the
* classes __construct function being called with no arguments. This is only an issue when
* the _as_object class does not have an explicit __construct method resulting in the
* cryptic error "Class %s does not have a constructor hence you cannot use ctor_params."
* In contrast, the same function call on PHP 5.5 will 'functionally' interpret
* _object_params == NULL as an omission of the third argument, resulting in the original
* intended functionally.
*
* Because the backing code for the mysql_fetch_object has not changed between 5.3 and 5.5,
* I suspect this discrepancy is due to the way the classes are instantiated on a boarder
* level. Additionally, mysql_fetch_object has been deprecated in 5.5 and should probably be
* replaced by mysqli_fetch_object or PDOStatement::fetch(PDO::FETCH_OBJ) in Kohana 3.4.
*/
if ($this->_object_params !== NULL)
{
// Return an object of given class name with constructor params
return mysql_fetch_object($this->_result, $this->_as_object, $this->_object_params);
}
else
{
// Return an object of given class name without constructor params
return mysql_fetch_object($this->_result, $this->_as_object);
}
}
else
{
// Return an array of the row
return mysql_fetch_assoc($this->_result);
}
}
public function seek($offset)
{
if ($this->offsetExists($offset) AND mysql_data_seek($this->_result, $offset))
{
// Set the current row to the offset
$this->_current_row = $this->_internal_row = $offset;
return TRUE;
}
else
{
return FALSE;
}
}
Return all of the rows in the result as an array.
// Indexed array of all rows
$rows = $result->as_array();
// Associative array of rows by "id"
$rows = $result->as_array('id');
// Associative array of rows, "id" => "name"
$rows = $result->as_array('id', 'name');
string
$key
= NULL - Column for associative keysstring
$value
= NULL - Column for valuesarray
public function as_array($key = NULL, $value = NULL)
{
$results = array();
if ($key === NULL AND $value === NULL)
{
// Indexed rows
foreach ($this as $row)
{
$results[] = $row;
}
}
elseif ($key === NULL)
{
// Indexed columns
if ($this->_as_object)
{
foreach ($this as $row)
{
$results[] = $row->$value;
}
}
else
{
foreach ($this as $row)
{
$results[] = $row[$value];
}
}
}
elseif ($value === NULL)
{
// Associative rows
if ($this->_as_object)
{
foreach ($this as $row)
{
$results[$row->$key] = $row;
}
}
else
{
foreach ($this as $row)
{
$results[$row[$key]] = $row;
}
}
}
else
{
// Associative columns
if ($this->_as_object)
{
foreach ($this as $row)
{
$results[$row->$key] = $row->$value;
}
}
else
{
foreach ($this as $row)
{
$results[$row[$key]] = $row[$value];
}
}
}
$this->rewind();
return $results;
}
Get a cached database result from the current result iterator.
$cachable = serialize($result->cached());
Database_Result_Cached
public function cached()
{
return new Database_Result_Cached($this->as_array(), $this->_query, $this->_as_object);
}
Implements Countable::count, returns the total number of rows.
echo count($result);
integer
public function count()
{
return $this->_total_rows;
}
Return the named column from the current row.
// Get the "id" value
$id = $result->get('id');
string
$name
required - Column to getmixed
$default
= NULL - Default value if the column does not existmixed
public function get($name, $default = NULL)
{
$row = $this->current();
if ($this->_as_object)
{
if (isset($row->$name))
return $row->$name;
}
else
{
if (isset($row[$name]))
return $row[$name];
}
return $default;
}
Implements Iterator::key, returns the current row number.
echo key($result);
integer
public function key()
{
return $this->_current_row;
}
Implements Iterator::next, moves to the next row.
next($result);
$this
public function next()
{
++$this->_current_row;
return $this;
}
Implements ArrayAccess::offsetExists, determines if row exists.
if (isset($result[10]))
{
// Row 10 exists
}
int
$offset
required - $offsetboolean
public function offsetExists($offset)
{
return ($offset >= 0 AND $offset < $this->_total_rows);
}
Implements ArrayAccess::offsetGet, gets a given row.
$row = $result[10];
int
$offset
required - $offsetmixed
public function offsetGet($offset)
{
if ( ! $this->seek($offset))
return NULL;
return $this->current();
}
Implements ArrayAccess::offsetSet, throws an error.
You cannot modify a database result.
int
$offset
required - $offsetmixed
$value
required - $valuevoid
final public function offsetSet($offset, $value)
{
throw new Kohana_Exception('Database results are read-only');
}
Implements ArrayAccess::offsetUnset, throws an error.
You cannot modify a database result.
int
$offset
required - $offsetvoid
final public function offsetUnset($offset)
{
throw new Kohana_Exception('Database results are read-only');
}
Implements Iterator::prev, moves to the previous row.
prev($result);
$this
public function prev()
{
--$this->_current_row;
return $this;
}
Implements Iterator::rewind, sets the current row to zero.
rewind($result);
$this
public function rewind()
{
$this->_current_row = 0;
return $this;
}
Implements Iterator::valid, checks if the current row exists.
This method is only used internally.
boolean
public function valid()
{
return $this->offsetExists($this->_current_row);
}