Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with mysqli::get_result? [duplicate]

Tags:

php

mysqli

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()

like image 966
einstein Avatar asked Aug 20 '11 17:08

einstein


Video Answer


3 Answers

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.

like image 187
vstm Avatar answered Sep 19 '22 22:09

vstm


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()

like image 27
Mohsenme Avatar answered Sep 21 '22 22:09

Mohsenme


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)
like image 42
genesis Avatar answered Sep 21 '22 22:09

genesis