5

I have the following code:

$postId = $_GET['postId'];
$mysqli = new mysqli('localhost', 'username', 'database', 'name_db');
mysqli_report(MYSQLI_REPORT_ALL);

$stmt = $mysqli->stmt_init();
$stmt->prepare("
SELECT *
FROM posts
WHERE postId = ?
");
$stmt->bind_param('i', $postId);
$stmt->execute();
$result = $stmt->get_result();//Call to undefined method 
$info = $result->fetch_array(MYSQLI_ASSOC);
echo json_encode($info);

And I get some error marked above. What have i done wrong?

EDIT:

changed fecth_array() to fetch_array()

einstein
  • 11,631
  • 25
  • 77
  • 98

6 Answers6

15

As others have stated, it is only available in bleeding edge PHP. You could do something like this (answer to a similar question):

function bind_array($stmt, &$row) {
    $md = $stmt->result_metadata();
    $params = array();
    while($field = $md->fetch_field()) {
        $params[] = &$row[$field->name];
    }

    call_user_func_array(array($stmt, 'bind_result'), $params);
}

// ....
bind_array($stmt, $info);
$stmt->fetch();

echo json_encode($info);

Or use mysqli::query if you have a simple query with no parameters - don't use it with dynamically generated SQL-statements.

Community
  • 1
  • 1
vstm
  • 11,709
  • 1
  • 44
  • 45
  • 10
    Please don't recommend "downgrading" from prepared statements to bogstandard strings. It's hard enough getting people to go the other way. – Lightness Races in Orbit Aug 20 '11 at 18:22
  • @Tomalak Geret'kal: right - thanks for the reminder, I've added a disclaimer. Not sure if I should remove the reference completely. – vstm Aug 20 '11 at 18:33
  • 1
    It is weird that "downgrading" is the only real option PHP gave us on some situations. – Dan Oct 08 '13 at 21:58
  • 1
    I tried to use wile stms->fetch() but it's skipping the first record. How can we loop over all result records? – Lamar Jun 05 '16 at 10:32
6

As mentioned in php documentation mysqli_stmt::get_result, this method is supported since PHP 5.3.0.

And it is stated in the user notes section that:

This method requires the mysqlnd driver. Othervise you will get this error: Call to undefined method mysqli_stmt::get_result()

Mohsenme
  • 942
  • 9
  • 20
2

You're probably using old version of PHP not supporting get_result() as stated on manual page

(No version information available, might only be in SVN)
genesis
  • 48,512
  • 18
  • 91
  • 118
2

The manual page doesn't give any clear information on which minimum version of PHP is needed for get_result() to work:

(No version information available, might only be in SVN)

I don't know the background to this, but it may simply not be available in your PHP version.

You could use fetch() instead.

Pekka
  • 418,526
  • 129
  • 929
  • 1,058
  • Ok, I thinkt that might be the case. But can I ask how I can get rows in a result set using mysqli and prepared statements? – einstein Aug 20 '11 at 18:03
  • I don't like using fetch, because I have so much columns. Is there another way? – einstein Aug 20 '11 at 18:05
  • @Woho [`fetch_array()`](http://www.php.net/manual/en/mysqli-result.fetch-array.php) for example – Pekka Aug 20 '11 at 18:07
0

check to see it your PHP mysql native driver is enable. get->result() only works when mysql native driver is enable. http://www.php.net/manual/en/mysqli-stmt.get-result.php

Ifetayo
  • 73
  • 1
  • 7
0

I´m guessing it´s the line below that, change:

$info = $result->fecth_array(MYSQLI_ASSOC);

to:

$info = $result->fetch_array(MYSQLI_ASSOC);
jeroen
  • 88,615
  • 21
  • 107
  • 128