Extending the MySQLi_Result
<?php
class Database_MySQLi extends MySQLi
{
public function query($query)
{
$this->real_query($query);
return new Database_MySQLi_Result($this);
}
}
class Database_MySQLi_Result extends MySQLi_Result
{
public function fetch()
{
return $this->fetch_assoc();
}
public function fetchAll()
{
$rows = array();
while($row = $this->fetch())
{
$rows[] = $row;
}
return $rows;
}
}
?>
La clase mysqli_result
(PHP 5)
Introducción
Representa el conjunto de resultados obtenidos a partir de una consulta en la base de datos.
Historial de cambios
| Versión | Descripción |
|---|---|
| 5.4.0 | Se añadió el soporte para Iterator, y mysqli_result ahora implementa Traversable. |
Sinopsis de la Clase
mysqli_result
implements
Traversable
{
/* Propiedades */
int $current_field
;
int $field_count;
array $lengths;
int $num_rows;
/* Métodos */
}Tabla de contenidos
- mysqli_result::$current_field — Obtener posición del campo actual de un puntero a un resultado
- mysqli_result::data_seek — Ajustar el puntero de resultado a una fila arbitraria del resultado
- mysqli_result::fetch_all — Obtener todas las filas en un array asociativo, numérico, o en ambos
- mysqli_result::fetch_array — Obtiene una fila de resultados como un array asociativo, numérico, o ambos
- mysqli_result::fetch_assoc — Obtiene una fila de resultado como un array asociativo
- mysqli_result::fetch_field_direct — Obtener los metadatos de un único campo
- mysqli_result::fetch_field — Retorna el próximo campo del resultset
- mysqli_result::fetch_fields — Devuelve un array de objetos que representan los campos de un conjunto de resultados
- mysqli_result::fetch_object — Devuelve la fila actual de un conjunto de resultados como un objeto
- mysqli_result::fetch_row — Obtener una fila de resultados como un array enumerado
- mysqli_result::$field_count — Obtiene el número de campos de un resultado
- mysqli_result::field_seek — Establecer el puntero del resultado al índice del campo especificado
- mysqli_result::free — Libera la memoria asociada a un resultado
- mysqli_result::$lengths — Retorna los largos de las columnas de la fila actual en el resultset
- mysqli_result::$num_rows — Obtiene el número de filas de un resultado
blar at blar dot de ¶
4 years ago
sinisaculic at gmail dot com ¶
2 years ago
storing this object in ANY kind will result in storing an empty object... if you try to serialize it dump it or do anything with it you will end up with a empty object.
you have to pull all data out f the object and then store the data... no other way.
Anonymous ¶
3 years ago
Generally, it appears Mysqli OO vs Procedural style has no significant difference in speed, at least with the more generally used functions and methods (connect, close, query, free, etc).
With the fetch_* family of functions and methods dealing with result rows, however, Procedural wins out. Averaging over a hundred or so tests with a result set of 180,000 records, and using mysqli_fetch_*() functions vs. their mysqli_result::fetch_*() counterpart object methods to read and iterate over all records, all of the mysqli_fetch_*() functions win by ~0.1 seconds less.
This is interesting considering we're dealing with the same result object in both styles.
This was using Vistax64, PHP5.3.2, Mysql 5.1.45, using a bit of this code:
<?php
// procedural - takes 0.1 seconds less than OO here
$stopwatch = microtime(true);
while($row = mysqli_fetch_assoc($result)){
++$z;
}
echo microtime(true) - $stopwatch;
// OO
$stopwatch = microtime(true);
while($row = $result->fetch_assoc()){
++$z;
}
echo microtime(true) - $stopwatch;
?>
tuxedobob ¶
9 months ago
Converting an old project from using the mysql extension to the mysqli extension, I found the most annoying change to be the lack of a corresponding mysql_result function in mysqli. While mysql_result is a generally terrible function, it was useful for fetching a single result field *value* from a result set (for example, if looking up a user's ID).
The behavior of mysql_result is approximated here, though you may want to name it something other than mysqli_result so as to avoid thinking it's an actual, built-in function.
<?php
function mysqli_result($res, $row, $field=0) {
$res->data_seek($row);
$datarow = $res->fetch_array();
return $datarow[$field];
}
?>
Implementing it via the OO interface is left as an exercise to the reader.
