预准备语句 SELECT 调用非对象上的成员函数 fetch_assoc()


prepared statement SELECT gives Call to a member function fetch_assoc() on a non-object

我正在尝试使用预准备语句获取数据库,但是我收到"在非对象上调用成员函数fetch_assoc()"错误。

我做错了什么?

感谢您的帮助!!

$peopleID = $_GET['peopleID'];  
$stmt = $link->prepare("SELECT * FROM people WHERE peopleID = ?");
$stmt->bind_param('i', $peopleID);

$result = $stmt->execute();
$stmt->store_result();
if ($stmt->num_rows >= "1") { 
while($row = $result->fetch_assoc()) {
    $firstname = $row ['firstname'];
    $lastname = $row ['lastname'];
}
}

mysqli_stmt::execute() 返回布尔值(真/假),而不是mysqli_result
从 php 5.3 开始,您可以使用 mysqli_stmt::get_result 从语句实例获取mysqli_result。

$stmt = $link->prepare("SELECT * FROM people WHERE peopleID = ?");
if ( !$stmt ) {
    yourErrorHandler();
}
else if ( !$stmt->bind_param('i', $_GET['peopleID']) ) {
    yourErrorHandler();
}
else if ( !$stmt->execute() ) {
    yourErrorHandler();
}
else {
    $result = $stmt->get_result();

    ...
}