0

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>
Community
  • 1
  • 1
Monique
  • 11
  • 3
  • what parameters are you giving in get_result() function? – Hamza Zafeer Mar 12 '16 at 12:46
  • Check your `phpinfo()` to see if you have the mysqlnd driver installed with the mysqli extension – frosty Mar 12 '16 at 12:59
  • @frosty : The next I got from phpinfo(): php version = 5.6.15 In configure command: '--with-mysqli=mysqlnd' configuration - mysql: - Client API library version: mysqlnd 5.0.11-dev - 20120503 configuration -mysqlnd: - Version: mysqlnd 5.0.11-dev - 20120503 - Loaded plugins: mysqlnd,debug_trace,auth_plugin_mysql_native_password,auth_plugin_mysql_clear_password,auth_plugin_sha256_password - API Extensions: mysqli,mysql,pdo_mysql. Seems ok!? – Monique Mar 12 '16 at 19:16
  • @HamzaZafeer : I'm not sure I understand your question. In the examples I looked into, including on the official php.net manual no parameters are given to get_result() ? – Monique Mar 12 '16 at 19:36

1 Answers1

1

I finally found the problem. Through this post: Example of how to use bind_result vs get_result and the comment of @Coolen. In the get_result version the line: $stmt->store_result(); should be removed!

Community
  • 1
  • 1
Monique
  • 11
  • 3