0

I have two tables in my database; Users & friends. I am trying to fetch the information of all users which are friends with the user performing the query. The problem is that my php code is throwing a "commands out of sync error". Furthermore, the code I have used looks a bit "hacky", please also suggest some other alternative, if any.

The error is thrown in the second line after the foreach loop.

MY CODE:

$friends = [];
$query = "SELECT friend FROM friends WHERE register_id=? AND status=1";
$stmt = $con->prepare($query);
$stmt->bind_param("i",$register_id);
$stmt->execute();
$stmt->bind_result($_friend);
while($stmt->fetch())
    array_push($friends,$_friend);
$stmt->close();
$query = "SELECT register_id FROM friends WHERE friend=? AND status=1";
$stmt = $con->prepare($query);
$stmt->bind_param("i",$register_id);
$stmt->execute();
$stmt->bind_result($_fri);
while($stmt->fetch())
    array_push($friends,$_fri);
$stmt->close();
$con->next_result();
foreach($friends as $id){
    $query="SELECT username,image,location FROM users WHERE register_id=?";
    $stmt = $con->prepare($query);<----- Error thrown here
    $stmt->bind_param("i",$id);
    $stmt->execute();
    $stmt->bind_result($_username,$_image,$_location);
    $stmt->fetch();
    $arr = [
        'name'=>$_username,
        'image'=>$_image,
        'location'=>$_location,
    ];
    $contacts[] = $arr;
}
sendJson(true,"Friends fetched successfully",$contacts);

WHAT I'VE TRIED:

I have tried to use

$stmt->store_result();
$stmt->close();
$con->next_result();
Salman Tariq
  • 353
  • 1
  • 11
  • Possible duplicate of [Commands out of sync; you can't run this command now](http://stackoverflow.com/questions/614671/commands-out-of-sync-you-cant-run-this-command-now) – Chris Apr 10 '16 at 21:19
  • I have tried that. No dice. Same error. – Salman Tariq Apr 10 '16 at 21:20
  • You're saying you've made sure you've consumed all the results of the query in your `foreach` loop? – Chris Apr 10 '16 at 21:25
  • Is `register_id` the primary key of the `users` table? – Chris Apr 10 '16 at 21:26
  • error is thrown in the second line after the foreach loop starts. I've marked said line. – Salman Tariq Apr 10 '16 at 21:29
  • yes. it is also the foreign key in "friends" table. – Salman Tariq Apr 10 '16 at 21:29
  • Can you check what iteration of the loop the problem occurs on? If it's on the first iteration the problem is with your previous query, if it's on the second the problem is with the query in the loop. – Chris Apr 10 '16 at 21:34
  • I didn't even think of that. I was busy trying to fix the previous query >_> . Thank you for you help. It worked. If you can answer, I will accept :) – Salman Tariq Apr 10 '16 at 21:36
  • and is there a way to compile all these queries into one? any kind of condition in sql, perhaps? – Salman Tariq Apr 10 '16 at 21:38
  • 1
    I haven't really answered it, just provided a debugging suggestion. If you have managed to solve it post the solution as a self-answer to help other people in future. – Chris Apr 10 '16 at 21:39
  • And yes, you could combine those queries in to one using a [JOIN](http://dev.mysql.com/doc/refman/5.7/en/join.html). – Chris Apr 10 '16 at 21:39

1 Answers1

2

The problem was in the foreach loop. Adding $stmt->close at the end of the loop fixed the problem.

Salman Tariq
  • 353
  • 1
  • 11