Fatal error: Call to undefined method mysqli_stmt:

2019-02-05 09:45发布

问题:

This question already has an answer here:

  • Call to undefined method mysqli_stmt::get_result 10 answers

My following php MYSQLi is not working, PHP version 5.9

    $query = $conn->prepare("SELECT * FROM users WHERE token= ? LIMIT 1");
    $query->bind_param('s',$cvalue);
    $query->execute();
    $result = $query->get_result();
    $row = $result->fetch_assoc();

It's giving me the following error:

Fatal error: Call to undefined method mysqli_stmt::get_result()

Where is the error? How can I fix it? Thanks

回答1:

This is too long for a comment.

Try this:

if($statement=$conn->prepare("SELECT * FROM users WHERE token= ? LIMIT 1")){

     $statement-> bind_param('s',$cvalue);

     // Execute
     $statement-> execute();

     // Bind results
     $statement-> bind_result($token);

     // Fetch value
     while ( $statement-> fetch() ) {
          echo $token . "<br>";
     }

     // Close statement
     $statement-> close();
}

// Close entire connection
$conn-> close();

Now, if while ( $statement-> fetch() ) doesn't work quite like you want it to, try replacing it with while ( $statement-> fetch_assoc() ), the way you have it now.

  • N.B.: If this doesn't work for you, I will simply delete the answer.

Footnotes:

As Rocket Hazmat stated in a comment, and I quote: It requires both PHP 5.3+ and the mysqlnd driver.

So, make sure that the driver is installed.

  • http://php.net/manual/en/book.mysqlnd.php
  • http://php.net/manual/en/mysqlnd.install.php


回答2:

As stated, make sure mysqlnd is installed.

I have just spent an hour scouring all the answers on StackOverflow sorting this out because I missed out a vital step and my scripts still failed - the step was restarting the httpd daemon.

uninstall php-mysql install php-mysqlnd

For me, running Centos 5.6 and php55, this was a case of

yum remove -y php55w-mysql yum install -y php55w-mysqlnd

This all went perfectly but still my PHP was failing with the error "Call to undefined method mysqli_stmt::get_result"

So I restarted Apache

service httpd restart

and - TA-DA - it worked perfectly.



回答3:

If you are using Virtual Private Server

  1. Go to this directory /var/cpanel/easy/apache/rawopts

  2. And create a new file all_php5 [/var/cpanel/easy/apache/rawopts]# cat > all_php5

  3. add below text and close the file
    --with-mysqli=mysql

  4. Go Easy Apache v3.32.14 in WHM Customize basic profile and enable MySQL "Improved" extension in Exhaustive Options List and build.



标签: php mysql mysqli