PHP 7.0.6 Released

mysqli_stmt::get_result

mysqli_stmt_get_result

(PHP 5 >= 5.3.0, PHP 7)

mysqli_stmt::get_result -- mysqli_stmt_get_resultGets a result set from a prepared statement

Description

Object oriented style

mysqli_result mysqli_stmt::get_result ( void )

Procedural style

mysqli_result mysqli_stmt_get_result ( mysqli_stmt $stmt )

Call to return a result set from a prepared statement query.

Parameters

stmt

Procedural style only: A statement identifier returned by mysqli_stmt_init().

Return Values

Returns a resultset for successful SELECT queries, or FALSE for other DML queries or on failure. The mysqli_errno() function can be used to distinguish between the two types of failure.

MySQL Native Driver Only

Available only with mysqlnd.

Examples

Example #1 Object oriented style

<?php 

$mysqli 
= new mysqli("127.0.0.1""user""password""world"); 

if(
$mysqli->connect_error)
{
    die(
"$mysqli->connect_errno$mysqli->connect_error");
}

$query "SELECT Name, Population, Continent FROM Country WHERE Continent=? ORDER BY Name LIMIT 1";

$stmt $mysqli->stmt_init();
if(!
$stmt->prepare($query))
{
    print 
"Failed to prepare statement\n";
}
else
{
    
$stmt->bind_param("s"$continent);

    
$continent_array = array('Europe','Africa','Asia','North America');

    foreach(
$continent_array as $continent)
    {
        
$stmt->execute();
        
$result $stmt->get_result();
        while (
$row $result->fetch_array(MYSQLI_NUM))
        {
            foreach (
$row as $r)
            {
                print 
"$r ";
            }
            print 
"\n";
        }
    }
}

$stmt->close();
$mysqli->close();
?>

Example #2 Procedural style

<?php 

$link 
mysqli_connect("127.0.0.1""user""password""world"); 

if (!
$link)
{
    
$error mysqli_connect_error();
    
$errno mysqli_connect_errno();
    print 
"$errno$error\n";
    exit();
}

$query "SELECT Name, Population, Continent FROM Country WHERE Continent=? ORDER BY Name LIMIT 1";

$stmt mysqli_stmt_init($link);
if(!
mysqli_stmt_prepare($stmt$query))
{
    print 
"Failed to prepare statement\n";
}
else
{
    
mysqli_stmt_bind_param($stmt"s"$continent);

    
$continent_array = array('Europe','Africa','Asia','North America');

    foreach(
$continent_array as $continent)
    {
        
mysqli_stmt_execute($stmt);
        
$result mysqli_stmt_get_result($stmt);
        while (
$row mysqli_fetch_array($resultMYSQLI_NUM))
        {
            foreach (
$row as $r)
            {
                print 
"$r ";
            }
            print 
"\n";
        }
    }
}
mysqli_stmt_close($stmt);
mysqli_close($link);
?>

The above examples will output:

Albania 3401200 Europe 
Algeria 31471000 Africa 
Afghanistan 22720000 Asia 
Anguilla 8000 North America 

See Also

User Contributed Notes

Anonymous
2 years ago
I went through a lot of trouble on a server where mysqlnd wasn't available, and had a lot of headaches.

If you don't have mysqlnd installed/loaded whatever, you will get an undefined reference when trying to call "mysqli_stmt_get_result()".

I wrote my own mysqli_stmt_get_result() and a mysqli_result_fetch_array() to go with it.

<?php
class iimysqli_result
{
    public
$stmt, $nCols;
}   

function
iimysqli_stmt_get_result($stmt)
{
   
/**    EXPLANATION:
     * We are creating a fake "result" structure to enable us to have
     * source-level equivalent syntax to a query executed via
     * mysqli_query().
     *
     *    $stmt = mysqli_prepare($conn, "");
     *    mysqli_bind_param($stmt, "types", ...);
     *
     *    $param1 = 0;
     *    $param2 = 'foo';
     *    $param3 = 'bar';
     *    mysqli_execute($stmt);
     *    $result _mysqli_stmt_get_result($stmt);
     *        [ $arr = _mysqli_result_fetch_array($result);
     *            || $assoc = _mysqli_result_fetch_assoc($result); ]
     *    mysqli_stmt_close($stmt);
     *    mysqli_close($conn);
     *
     * At the source level, there is no difference between this and mysqlnd.
     **/
   
$metadata = mysqli_stmt_result_metadata($stmt);
   
$ret = new iimysqli_result;
    if (!
$ret) return NULL;

   
$ret->nCols = mysqli_num_fields($metadata);
   
$ret->stmt = $stmt;

   
mysqli_free_result($metadata);
    return
$ret;
}

function
iimysqli_result_fetch_array(&$result)
{
   
$ret = array();
   
$code = "return mysqli_stmt_bind_result(\$result->stmt ";

    for (
$i=0; $i<$result->nCols; $i++)
    {
       
$ret[$i] = NULL;
       
$code .= ", \$ret['" .$i ."']";
    };

   
$code .= ");";
    if (!eval(
$code)) { return NULL; };

   
// This should advance the "$stmt" cursor.
   
if (!mysqli_stmt_fetch($result->stmt)) { return NULL; };

   
// Return the array we built.
   
return $ret;
}
?>

Hope this helps someone.
jari dot wiklund at gmail dot com
4 years ago
Please note that this method requires the mysqlnd driver. Othervise you will get this error: Call to undefined method mysqli_stmt::get_result()
chris at hexus dot io
9 months ago
This method drove me mad for an hour or two because it returns <b>false</b> after performing a successful query that isn't a SELECT, even though the documentation says "return <b>FALSE</b> on failure". Nothing failed!

If you're preparing a SELECT statement, expect a result object. Anything else, just use the properties of the statement object.

I find this library painfully inconsistent.
tonywoodecode at gmail dot com
5 months ago
well I for one found anonymous' above eval-trick code very useful, seeing as no one has posted a working alternative! However, because I wanted to cater for a generic database object, I found it a problem that the code assumed numeric array was fine for all callsites, and I needed to cater for all callers using assoc arrays exlusively. I came up with this:
<?php
class IImysqli_result {
        public
$stmt, $ncols;
}  

class
DBObject {

    function
iimysqli_get_result($stmt) {
     
$metadata = $stmt->result_metadata();
     
$ret = new IImysqli_result;
      if (!
$ret || !$metadata) return NULL; //the latter because this gets called whether we are adding/updating as well as returning
     
$ret->ncols = $metadata->field_count;
     
$ret->stmt = $stmt;
     
$metadata->free_result();
      return
$ret;
   }

  
//this mimics mysqli_fetch_array by returning a new row each time until exhausted
   
function iimysqli_result_fetch_array(&$result) {
     
$stmt = $result->stmt;
     
$stmt->store_result();
     
$resultkeys = array();
     
$thisName = "";
      for (
$i = 0; $i < $stmt->num_rows; $i++ ) {
           
$metadata = $stmt->result_metadata();
            while (
$field = $metadata->fetch_field() ) {
               
$thisName = $field->name;
               
$resultkeys[] = $thisName;
            }
      }

     
$ret = array();
     
$code = "return mysqli_stmt_bind_result(\$result->stmt ";
      for (
$i=0; $i<$result->ncols; $i++) {
         
$ret[$i] = NULL;
         
$theValue = $resultkeys[$i];
         
$code .= ", \$ret['$theValue']";
      }

     
$code .= ");";
      if (!eval(
$code)) {
        return
NULL;
      }

     
// This should advance the "$stmt" cursor.
     
if (!mysqli_stmt_fetch($result->stmt)) {
        return
NULL;
      }

     
// Return the array we built.
     
return $ret;
    }
}

?>
To Top