调用未定义的方法 mysqli_stmt::get_result

2022-08-30 07:10:49

这是我的代码:

include 'conn.php';
$conn = new Connection();
$query = 'SELECT EmailVerified, Blocked FROM users WHERE Email = ? AND SLA = ? AND `Password` = ?';
$stmt = $conn->mysqli->prepare($query);
$stmt->bind_param('sss', $_POST['EmailID'], $_POST['SLA'], $_POST['Password']);
$stmt->execute();
$result = $stmt->get_result();

我在最后一行收到错误:调用未定义的方法mysqli_stmt::get_result()

以下是 conn.php 的代码:

define('SERVER', 'localhost');
define('USER', 'root');
define('PASS', 'xxxx');
define('DB', 'xxxx');
class Connection{
    /**
     * @var Resource 
     */
    var $mysqli = null;

    function __construct(){
        try{
            if(!$this->mysqli){
                $this->mysqli = new MySQLi(SERVER, USER, PASS, DB);
                if(!$this->mysqli)
                    throw new Exception('Could not create connection using MySQLi', 'NO_CONNECTION');
            }
        }
        catch(Exception $ex){
            echo "ERROR: ".$e->getMessage();
        }
    }
}

如果我写这行:

if(!stmt) echo 'Statement prepared'; else echo 'Statement NOT prepared';

它打印“声明未准备”。如果我直接在IDE中运行查询,请替换?标记与值,它工作正常。请注意,$conn对象在项目中的其他查询中工作正常。

任何帮助请.


答案 1

请阅读此方法的用户说明:

http://php.net/manual/en/mysqli-stmt.get-result.php

它需要 mysqlnd 驱动程序...如果它没有安装在你的网络空间上,你将不得不使用bind_result()fetch()


答案 2

因此,如果MySQL本机驱动程序(mysqlnd)驱动程序不可用,因此使用bind_result提取而不是get_result,则代码将变为:

include 'conn.php';
$conn = new Connection();
$query = 'SELECT EmailVerified, Blocked FROM users WHERE Email = ? AND SLA = ? AND `Password` = ?';
$stmt = $conn->mysqli->prepare($query);
$stmt->bind_param('sss', $_POST['EmailID'], $_POST['SLA'], $_POST['Password']);
$stmt->execute();
$stmt->bind_result($EmailVerified, $Blocked);
while ($stmt->fetch())
{
   /* Use $EmailVerified and $Blocked */
}
$stmt->close();
$conn->mysqli->close();

推荐