简体   繁体   English

mysqli :: get_result有什么问题?

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

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

As others have stated, it is only available in bleeding edge PHP. 正如其他人所述,它仅在最新的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 . 或者,如果您有一个没有参数的简单查询,请使用mysqli :: query- 不要在动态生成的SQL语句中使用它

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 notes section that: 用户说明部分中指出:

This method requires the mysqlnd driver. 此方法需要mysqlnd驱动程序。 Othervise you will get this error: Call to undefined method mysqli_stmt::get_result() 否则您将收到此错误:调用未定义的方法mysqli_stmt :: get_result()

您可能正在使用旧版本的PHP,但不支持手册页上所述的get_result()

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

The manual page doesn't give any clear information on which minimum version of PHP is needed for get_result() to work: 手册页上没有任何明确的信息说明get_result()所需的最低PHP版本:

(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()代替。

check to see it your PHP mysql native driver is enable. 检查以查看您的PHP mysql本机驱动程序已启用。 get->result() only works when mysql native driver is enable. get-> result()仅在启用mysql本机驱动程序时有效。 http://www.php.net/manual/en/mysqli-stmt.get-result.php http://www.php.net/manual/zh/mysqli-stmt.get-result.php

I´m guessing it´s the line below that, change: 我猜这是下面的一行,更改:

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

to: 至:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM