3

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

Paulie-C
  • 1,674
  • 1
  • 13
  • 29
rksh
  • 3,920
  • 10
  • 49
  • 68
  • 2
    `mysqli_stmt :: get_result` is available only with mysqlnd. http://php.net/manual/en/mysqli-stmt.get-result.php - Did you Google your error message? – Funk Forty Niner Oct 15 '14 at 18:37
  • @Fred-ii- isn't it available by default from PHP 5.3? Note: mysqli_stmt::get_result() is only available at PHP v5.3.0 or above Found it here http://stackoverflow.com/questions/15659326/php-mysqli-how-can-i-rewrite-fetch-to-fetch-assoc-like-concat – rksh Oct 15 '14 at 18:39
  • Now that you mention it, yes. Which I find strange. See this Q&A http://stackoverflow.com/q/21223268/ - and http://stackoverflow.com/q/13659856/ and I'll see if I can't find you more. – Funk Forty Niner Oct 15 '14 at 18:40
  • @Fred-ii- So is there any other way of getting the result from the execute by using fetch_assoc() without using get_result? – rksh Oct 15 '14 at 18:46
  • I can post an answer below, will be too long in comments. You can give it a try. How's that sound? – Funk Forty Niner Oct 15 '14 at 18:52
  • @rksh: It requires **both** PHP 5.3+ *and* the mysqlnd driver. – gen_Eric Oct 15 '14 at 18:55

3 Answers3

5

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.

Hello World
  • 2,673
  • 7
  • 28
  • 60
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
4

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.

KayCee
  • 115
  • 7
  • it looks like its not the answer to this quesiton – Gopal Singh Sirvi Oct 30 '15 at 13:04
  • I had exactly the same question and behaviour : : Call to undefined method mysqli_stmt::get_result(). The problem was no mysqlnd You can try all the other methods you like, but if you are using statements, you need the nd driver. If you want the nd driver, simply installing it wont work - you need to restart httpd. The accepted answer re binding results is (IMHO) misdirection. it's a driver install and config problem. – KayCee Nov 18 '15 at 13:50
1

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.

Hasan Jamshaid
  • 1,659
  • 1
  • 11
  • 14