Implements: ArrayAccess | SeekableIterator | Traversable | Iterator | Countable
MySQLi database result. See Results for usage and examples.
Class declared in MODPATH/database/classes/Database/MySQLi/Result.php on line 3.
$_as_objectlink to thisNULL
$_current_rowlink to thisinteger 0
$_internal_rowlink to thisinteger 0
$_object_paramslink to thisNULL
$_querylink to thisNULL
$_resultlink to thisNULL
$_total_rowslink to thisinteger 0
Sets the total number of rows and stores the result locally.
mixed
$result
required - Query result string
$sql
required - SQL query mixed
$as_object
= bool FALSE - $as_object array
$params
= NULL - $params void
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;
}
Result destruction cleans up all open result sets.
void
public
function
__destruct()
{
if
(
is_resource
(
$this
->_result)) {
$this
->_result->free();
}
}
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
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;
}
}
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 keys string
$value
= NULL - Column for values array
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
;
}
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 get mixed
$default
= NULL - Default value if the column does not exist mixed
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 - $offset boolean
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 - $offset mixed
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 - $offset mixed
$value
required - $value void
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 - $offset void
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);
}