致命错误:调用非对象上的成员函数 bind_param) 进行查询


Fatal error: Call to a member function bind_param() on a non-object for query

我有以下代码:

/* Select the latest 10 posts from users this person's following */
$stmt = $cxn->prepare('SELECT * FROM posts WHERE user_id IN (SELECT following_id FROM follows WHERE user_id = ?) ORDER BY datetime DESC LIMIT 15');
$stmt->bind_param('i', $user_id);
$stmt->execute();
$result = $stmt->get_result();
/* If a result exists, continue. */
if ($result->num_rows) {
    while ($row = $result->fetch_assoc()) {
        $stmt = $cxn->prepare('SELECT username FROM users WHERE user_id = ?');
        $stmt->bind_param('i', $row['user_id']);
        $stmt->execute();
        $stmt->bind_result($username);
        $stmt->fetch();
    }
}

问题是我在第二个查询的bind_param行上收到以下错误:

Fatal error: Call to a member function bind_param() on a non-object on line $stmt->bind_param('i', $row['user_id']);

怎么了?请帮忙!

查询字符串中存在错误。您可以回显出mysql错误,以更清楚地了解正在发生的事情:

$stmt = $cxn->prepare('SELECT * FROM posts WHERE user_id IN (SELECT following_id FROM follows WHERE user_id = ?) ORDER BY datetime DESC LIMIT 15');
echo $cxn->error;
$stmt->bind_param('i', $user_id);