MYSQLi bind_result is returning null

2019-01-29 06:23发布

问题:

I am trying to output the variables that I get from the database in my query but nothing is being returned. Using MYSQLi prepared statements.

Please see code below:

$stmt = $con->prepare("SELECT first_name, last_name FROM transactions WHERE order_id = ?");
$stmt->bind_param('i', $order_id);
$stmt->execute(); 
$stmt->store_result();
$stmt->bind_result($first_name, $last_name);
$stmt->close();


// Output review live to page 
echo $first_name;

I cannot see where I am going wrong? PS I am new to prepared statements so please go easy on me!

回答1:

you forgetting the line to fetch the result. fetch() .

try that:

  $stmt->bind_result($first_name, $last_name);
  $stmt->fetch();  // ----- > you forget that line to fetch results.
  $stmt->close();