Question

I am trying to make a simple select query with mysqli but it seems like it does not work. Does anyone see what is wrong here? I expect the two variables being printed out at the end of the file.

$stmt = $mysqli->prepare("select billnumber, amount from bill where billid = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($billnumber, $amount);
$stmt->fetch();

var_dump($mysqli);
echo "<br><br>";

var_dump($stmt);
echo "<br>";

echo $billnumber;
echo $amount;

$stmt->close();

Output:

object(mysqli)#2 (19) { ["affected_rows"]=> int(-1) ["client_info"]=> string(79) "mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $" ["client_version"]=> int(50011) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(2) ["host_info"]=> string(25) "Localhost via UNIX socket" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(6) "5.5.28" ["server_version"]=> int(50528) ["stat"]=> string(152) "Uptime: 2619401 Threads: 11 Questions: 220513466 Slow queries: 146 Opens: 14454832 Flush tables: 1 Open tables: 64 Queries per second avg: 84.184" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(3244658) ["warning_count"]=> int(0) } 

object(mysqli_stmt)#4 (10) { ["affected_rows"]=> NULL ["insert_id"]=> NULL ["num_rows"]=> NULL ["param_count"]=> NULL ["field_count"]=> NULL ["errno"]=> NULL ["error"]=> NULL ["error_list"]=> NULL ["sqlstate"]=> NULL ["id"]=> NULL } 
Was it helpful?

Solution

mysqli_stmt_close()

Closes a prepared statement. mysqli_stmt_close() also deallocates the statement handle. If the current statement has pending or unread results, this function cancels them so that the next query can be executed.

You shouldn't close before your read the results

OTHER TIPS

Try this:

$stmt = $mysqli->prepare("select billnumber, amount from bill where billid = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($billnumber, $amount);
while($stmt -> fetch()){

var_dump($billnumber);
echo "<br><br>";

var_dump($amount);
echo "<br>";

echo $billnumber;
echo $amount;
}
$stmt->close();

Let me know if it works :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top