php mysqli::get_result 有什么问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7133575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:06:46  来源:igfitidea点击:

What's wrong with mysqli::get_result?

phpmysqli

提问by einstein

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

将 fecth_array() 更改为 fetch_array()

回答by vstm

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

正如其他人所说,它仅在最前沿的 PHP 中可用。你可以做这样的事情(回答一个类似的问题):

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::queryif you have a simple query with no parameters - don't use it with dynamically generated SQL-statements.

或者,如果您有一个没有参数的简单查询,请使用mysqli::query-不要将它与动态生成的 SQL 语句一起使用

回答by Mohsenme

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

如 php 文档mysqli_stmt::get_result 中所述,自 PHP 5.3.0 起支持此方法。

And it is stated in the user notessection that:

并且在用户注释部分中指出:

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

此方法需要 mysqlnd 驱动程序。否则你会得到这个错误:Call to undefined method mysqli_stmt::get_result()

回答by genesis

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

您可能使用的是不支持 get_result() 的旧版 PHP,如手册页所述

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

回答by Pekka

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

手册页并不会给这是需要的PHP最低版本的任何明确的信息get_result()工作:

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

(没有可用的版本信息,可能只有在 SVN 中)

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

我不知道它的背景,但它可能在您的 PHP 版本中根本不可用。

You could use fetch()instead.

你可以用fetch()

回答by jeroen

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

我猜是下面的那一行,改变:

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

to:

到:

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

回答by Ifetayo

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

检查以查看您的 PHP mysql 本机驱动程序是否已启用。get->result() 仅在启用 mysql 本机驱动程序时有效。http://www.php.net/manual/en/mysqli-stmt.get-result.php