Kohana_Database_MySQLi_Result
extends Database_Result
extends Kohana_Database_Result
Implements: Countable | Iterator | Traversable | SeekableIterator | ArrayAccess
MySQLi database result. See Results for usage and examples.
Constants
- None
Properties
-
protected $_as_object
-
Default value:
NULL
-
protected $_current_row
-
Default value:
integer 0
-
protected $_internal_row
-
Default value:
integer 0
-
protected $_object_params
-
Default value:
NULL
-
protected $_query
-
Default value:
NULL
-
protected $_result
-
Default value:
NULL
-
protected $_total_rows
-
Default value:
integer 0
Methods
public __construct(mixed $result , string $sql [, mixed $as_object = bool FALSE , array $params = NULL ] ) (defined in Kohana_Database_MySQLi_Result)
Sets the total number of rows and stores the result locally.
Parameters
- mixed $result required - Query result
- string $sql required - SQL query
- mixed $as_object = bool FALSE - $as_object
- array $params = NULL - $params
Return Values
- void
Source Code
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 = $result->num_rows;
}
public __destruct() (defined in Kohana_Database_MySQLi_Result)
Result destruction cleans up all open result sets.
Return Values
- void
Source Code
public function __destruct()
{
if (is_resource($this->_result))
{
$this->_result->free();
}
}
public current() (defined in Kohana_Database_MySQLi_Result)
Source Code
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++;
if ($this->_as_object === TRUE)
{
// Return an stdClass
return $this->_result->fetch_object();
}
elseif (is_string($this->_as_object))
{
// Return an object of given class name
return $this->_result->fetch_object($this->_as_object, (array) $this->_object_params);
}
else
{
// Return an array of the row
return $this->_result->fetch_assoc();
}
}
public seek() (defined in Kohana_Database_MySQLi_Result)
Source Code
public function seek($offset)
{
if ($this->offsetExists($offset) AND $this->_result->data_seek($offset))
{
// Set the current row to the offset
$this->_current_row = $this->_internal_row = $offset;
return TRUE;
}
else
{
return FALSE;
}
}
public as_array([ string $key = NULL , string $value = NULL ] ) (defined in Kohana_Database_Result)
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');
Parameters
- string $key = NULL - Column for associative keys
- string $value = NULL - Column for values
Return Values
- array
Source Code
public function as_array($key = NULL, $value = NULL)
{
$results = [];
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;
}
public cached() (defined in Kohana_Database_Result)
Get a cached database result from the current result iterator.
$cachable = serialize($result->cached());
Tags
Return Values
- Database_Result_Cached
Source Code
public function cached()
{
return new Database_Result_Cached($this->as_array(), $this->_query, $this->_as_object);
}
public count() (defined in Kohana_Database_Result)
Implements Countable::count, returns the total number of rows.
echo count($result);
Return Values
- integer
Source Code
public function count()
{
return $this->_total_rows;
}
public get(string $name [, mixed $default = NULL ] ) (defined in Kohana_Database_Result)
Return the named column from the current row.
// Get the "id" value
$id = $result->get('id');
Parameters
- string $name required - Column to get
- mixed $default = NULL - Default value if the column does not exist
Return Values
- mixed
Source Code
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;
}
public key() (defined in Kohana_Database_Result)
Implements Iterator::key, returns the current row number.
echo key($result);
Return Values
- integer
Source Code
public function key()
{
return $this->_current_row;
}
public next() (defined in Kohana_Database_Result)
Implements Iterator::next, moves to the next row.
next($result);
Return Values
- $this
Source Code
public function next()
{
++$this->_current_row;
return $this;
}
public offsetExists(int $offset ) (defined in Kohana_Database_Result)
Implements ArrayAccess::offsetExists, determines if row exists.
if (isset($result[10]))
{
// Row 10 exists
}
Parameters
- int $offset required - $offset
Return Values
- boolean
Source Code
public function offsetExists($offset)
{
return ($offset >= 0 AND $offset < $this->_total_rows);
}
public offsetGet(int $offset ) (defined in Kohana_Database_Result)
Implements ArrayAccess::offsetGet, gets a given row.
$row = $result[10];
Parameters
- int $offset required - $offset
Return Values
- mixed
Source Code
public function offsetGet($offset)
{
if ( ! $this->seek($offset))
return NULL;
return $this->current();
}
final public offsetSet(int $offset , mixed $value ) (defined in Kohana_Database_Result)
Implements ArrayAccess::offsetSet, throws an error.
You cannot modify a database result.
Parameters
- int $offset required - $offset
- mixed $value required - $value
Tags
Return Values
- void
Source Code
final public function offsetSet($offset, $value)
{
throw new Kohana_Exception('Database results are read-only');
}
final public offsetUnset(int $offset ) (defined in Kohana_Database_Result)
Implements ArrayAccess::offsetUnset, throws an error.
You cannot modify a database result.
Parameters
- int $offset required - $offset
Tags
Return Values
- void
Source Code
final public function offsetUnset($offset)
{
throw new Kohana_Exception('Database results are read-only');
}
public prev() (defined in Kohana_Database_Result)
Implements Iterator::prev, moves to the previous row.
prev($result);
Return Values
- $this
Source Code
public function prev()
{
--$this->_current_row;
return $this;
}
public rewind() (defined in Kohana_Database_Result)
Implements Iterator::rewind, sets the current row to zero.
rewind($result);
Return Values
- $this
Source Code
public function rewind()
{
$this->_current_row = 0;
return $this;
}
public valid() (defined in Kohana_Database_Result)
Implements Iterator::valid, checks if the current row exists.
This method is only used internally.
Return Values
- boolean
Source Code
public function valid()
{
return $this->offsetExists($this->_current_row);
}