0

I have the following code:

/* 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();
    }
}

The issue is that I get the following error on the bind_param line of the second query:

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

What's wrong? Please help!

Burrows
  • 475
  • 2
  • 8
  • 18
  • In your very first line your `prepare` has failed. Check the return status for `false` and then check `$cxn->error` for more details –  Nov 04 '13 at 00:45
  • Try using another variable for your 2nd query, so $stmt1 for example – Mazzy Nov 04 '13 at 00:47
  • @Mazzy That didn't work. Same error. – Burrows Nov 04 '13 at 00:52
  • I'm guessing the `select` statement's syntax is bogus. Please provide the `create table users` statement from a dump. – geomagas Nov 04 '13 at 00:58

1 Answers1

1

There is an error in your Query string. You can echo out the mysql error to get a clearer picture of what's happening:

$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);
hammus
  • 2,602
  • 2
  • 19
  • 37