PHP 5.5.16 is released

The mysqli_result class

(PHP 5)

Introduzione

Represents the result set obtained from a query against the database.

Storico dei cambiamenti

Storico dei cambiamenti
Versione Descrizione
5.4.0 Iterator support was added, as mysqli_result now implements Traversable.

Sommario dellla classe

mysqli_result implements Traversable {
/* Proprietà */
array $lengths;
/* Metodi */
bool data_seek ( int $offset )
mixed fetch_all ([ int $resulttype = MYSQLI_NUM ] )
mixed fetch_array ([ int $resulttype = MYSQLI_BOTH ] )
array fetch_assoc ( void )
object fetch_field_direct ( int $fieldnr )
object fetch_field ( void )
array fetch_fields ( void )
object fetch_object ([ string $class_name = "stdClass" [, array $params ]] )
mixed fetch_row ( void )
bool field_seek ( int $fieldnr )
void free ( void )
}

Indice dei contenuti

add a note add a note

User Contributed Notes 6 notes

up
9
tuxedobob
2 years 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.
up
3
blar at blar dot de
5 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;
    }
}

?>
up
2
Marc17
6 months ago
An "mysqli_result" function where $field can be like table_name.field_name with alias or not.
<?php
function mysqli_result($result,$row,$field=0) {
    if (
$result===false) return false;
    if (
$row>=mysqli_num_rows($result)) return false;
    if (
is_string($field) && !(strpos($field,".")===false)) {
       
$t_field=explode(".",$field);
       
$field=-1;
       
$t_fields=mysqli_fetch_fields($result);
        for (
$id=0;$id<mysqli_num_fields($result);$id++) {
            if (
$t_fields[$id]->table==$t_field[0] && $t_fields[$id]->name==$t_field[1]) {
               
$field=$id;
                break;
            }
        }
        if (
$field==-1) return false;
    }
   
mysqli_data_seek($result,$row);
   
$line=mysqli_fetch_array($result);
    return isset(
$line[$field])?$line[$field]:false;
}
?>
up
1
sinisaculic at gmail dot com
3 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.
up
1
Anonymous
4 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;

?>
up
0
anonunfinder at gmail dot com
3 months ago
<?php
class MySQL
{
    protected
$mysql;
    function
__construct()
    {
               
//Get MySQL config values from config.ini file
       
if($config = parse_ini_file("../config.ini"))
        {
           
// Obtener los valores del fichero de configuración config.ini
           
$ip = $config["ip"];
           
$user = $config["usuario"];
           
$pass = $config["password"];
           
$bd = $config["bd"];
                         
                        
//Connection between a database and php
           
$this->mysql = new mysqli($ip, $user, $pass, $bd);
        }
    }

    function
setResultQuery($query, $param)
    {
       
$array = NULL;
        if(!
$this->mysql->connect_errno)
        {
           
$stmt = $this->setStatement($query, $param);
            try
            {
                if(
$stmt != NULL)
                {
                    if(
$stmt->execute())
                    {
                       
//Obtener resultados
                       
$stmt->store_result();
                       
$variables = array();
                       
$data = array();
                       
$meta = $stmt->result_metadata();
                        while(
$field = $meta->fetch_field())
                        {
                           
$variables[] = &$data[$field->name];
                        }
                       
call_user_func_array(array($stmt, 'bind_result'), $variables);
                       
$i=0;
                        while(
$stmt->fetch())
                        {
                               
$array[$i] = array();
                                foreach(
$data as $k=>$v)
                               
$array[$i][$k] = $v;
                               
$i++;
                        }
                       
$stmt->close();
                    }
                }
            }catch(
Exception $e){
               
$array = FALSE;
            }
        }
        return
$array;
    }

    function
setStatement($query, $param)
    {
        try
        {
           
$stmt = $this->mysql->prepare($query);
           
$ref = new ReflectionClass('mysqli_stmt');
            if(
count($param) != 0)
            {
               
               
$method = $ref->getMethod('bind_param');
               
$method->invokeArgs($stmt, $param);
            }
        }catch(
Exception $e){
            if(
$stmt != null)
            {
               
$stmt->close();
            }
        }
        return
$stmt;
    }

    function
setNoResultQuery($query, $param)
    {
       
$validation = FALSE;
        if(!
$this->mysql->connect_errno)
        {
            try
            {
               
$stmt = $this->setStatement($query, $param);
                if(
$stmt != null)
                {
                    if(
$stmt->execute())
                    {
                       
$stmt->close();
                       
$validacion = TRUE;
                    }
                }
            }catch(
Exception $e){
               
$validation = FALSE;
            }
        }
        return
$validation;
    }

    function
__destruct()
    {
       
$this->mysql->close();
    }
}
?>
To Top