I am struggling with this problem for some time now and searched everywhere for an answer. Post: Example of how to use bind_result vs get_result gives a very good explanation how to use the two different methods of retrieving a result of an executed prepared statement. This is also in line with other answers I found. I am having PHP version 5.6.15 so for get_result() this shouldn't be a problem either (only available from > 5.3).
I have an object oriented programming environment but for test sake a striped the examples to the bare procedural minimum with a select of one row from the database to find the error.
Result: my version with "bind_result()" works perfectly and returns:
ID: 1
Content: Test1
and the "get_result()" version returns:
"Call to a member function fetch_assoc() on boolean in ...".
I tried a lot of variations to get it working, but brought it back to the following minimum which should work, but it doesn't. Can somebody help?
my test table in the database had two columns: "id" and "content".
The working version with bind_result():
<!DOCTYPE html>
<html lang="en">
<?php
static $connection;
$config = parse_ini_file('../config.ini');
$connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
$query = "SELECT * FROM test WHERE id = ?";
$id = 1;
$stmt = $connection->prepare($query);
$stmt->bind_param('i',$id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id, $content);
while ($stmt->fetch()) {
echo 'ID: '.$id.'<br>';
echo 'Content: '.$content.'<br>';
}
$stmt->free_result();
$stmt->close();
$connection->close();
?>
</html>
So the version with get_result() that doesn't work:
<!DOCTYPE html>
<html lang="en">
<?php
static $connection;
$config = parse_ini_file('../config.ini');
$connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
$query = "SELECT * FROM test WHERE id = ?";
$id = 1;
$stmt = $connection->prepare($query);
$stmt->bind_param('i',$id);
$stmt->execute();
$stmt->store_result();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo 'ID: '.$row['id'].'<br>';
echo 'Content: '.$row['content'].'<br>';
}
$stmt->free_result();
$stmt->close();
$connection->close();
?>
</html>