How to get the result set from a prepared statement using MySQLi in PHP

1 Answer

0 votes
$db_host        = 'localhost';
$db_user        = 'root';
$db_password    = '';
$db_name        = 'allonpage';
 
$con = new mysqli('localhost', $db_user, $db_password, $db_name);
 
if ($con->connect_error) 
    die('Connection Error: ' . $con->server_info);
 
$sql = "
  SELECT src, alt
  FROM images
";    
     
if ($stmt = $con->prepare($sql)) 
{
    $stmt->execute();
    
    $stmt->bind_result($src, $alt); 
    
    // Get the result set from a prepared statement
    $result = $stmt->get_result();
    while ($row = $result->fetch_array(MYSQLI_NUM))
    {
        foreach ($row as $data)
        {
            echo $data . " - ";
        }
        echo "<br />";
    }
    
    $stmt->close();
}
                                  
$con->close();
 
/*
run: 
 
http://www.ws.com/images/us-flag.gif - us flag 
http://www.ws.com/images/logo.png - logo
...
 
*/

 



answered Jul 11, 2016 by avibootz
...