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.
La classe mysqli_result
(PHP 5)
Introduction
Représente le jeu de résultats obtenu depuis une requête.
Historique
| Version | Description |
|---|---|
| 5.4.0 | Le support Iterator a été ajouté, et mysqli_result implémente maintenant Traversable. |
Synopsis de la classe
mysqli_result
implements
Traversable
{
/* Propriétés */
int $current_field
;
int $field_count;
array $lengths;
int $num_rows;
/* Méthodes */
}Sommaire
- mysqli_result::$current_field — Récupère la position courante d'un champ dans un pointeur de résultat
- mysqli_result::data_seek — Déplace le pointeur interne de résultat
- mysqli_result::fetch_all — Lit toutes les lignes de résultats dans un tableau
- mysqli_result::fetch_array — Retourne une ligne de résultat sous la forme d'un tableau associatif, d'un tableau indexé, ou les deux
- mysqli_result::fetch_assoc — Récupère une ligne de résultat sous forme de tableau associatif
- mysqli_result::fetch_field_direct — Récupère les métadonnées d'un champ unique
- mysqli_result::fetch_field — Retourne le prochain champs dans le jeu de résultats
- mysqli_result::fetch_fields — Retourne un tableau d'objets représentant les champs dans le résultat
- mysqli_result::fetch_object — Retourne la ligne courante d'un jeu de résultat sous forme d'objet
- mysqli_result::fetch_row — Récupère une ligne de résultat sous forme de tableau indexé
- mysqli_result::$field_count — Récupère le nombre de champs dans un résultat
- mysqli_result::field_seek — Déplace le pointeur de résultat sur le champ spécifié
- mysqli_result::free — Libère la mémoire associée à un résultat
- mysqli_result::$lengths — Retourne la longueur des colonnes de la ligne courante du jeu de résultats
- mysqli_result::$num_rows — Retourne le nombre de lignes dans un résultat
tuxedobob ¶
9 months 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;
?>
blar at blar dot de ¶
4 years ago
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;
}
}
?>
