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

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

我正在尝试使用准备好的语句获取数据库,但出现 "Call to a member function fetch_assoc() on a non-object " 错误。

我做错了什么?

感谢您的帮助!!

$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() returns a bool (true/false), not a 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();


    ...
}