Reputation: 59
I'm probably going in the complete wrong direction but I want to get an array of data from the database and then use that array in another SQL statement.
Here is my current code:
$result = mysql_query($query_friend_club_count) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$row['idPerson']. " - ". $row['idFriend'];
$idFriend = $row['idFriend'];
$array = $idFriend',';
$query_friends = "SELECT * FROM whosout WHERE idPerson IN ('$array')";
$query_friends_run = mysql_query($query_friends);
$friendCounter = mysql_num_rows($query_friends_run);
echo $friendCounter;
}
I'm getting a error of:
syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
Don't know if that helps.
Any suggestions would be really helpful as I've been stuck on this for ages!!
Upvotes: 0
Views: 69
Reputation: 41885
You can also group them first instead of quering them each:
$array = array();
while($row = mysql_fetch_array($result)){
$array[] = mysql_escape_string($row['idFriend']); // escape just to be sure
}
$array = "'".implode("','", $array) . "'"; // comma separated values
$query_friends = "SELECT * FROM whosout WHERE idPerson IN ($array)";
$query_friends_run = mysql_query($query_friends);
$friendCounter = mysql_num_rows($query_friends_run);
echo $friendCounter;
Or if this column is an INT, no need for quotes:
$array = implode(', ', $array);
Upvotes: 4
Reputation: 11
Correct way is:
$arr = array();
while($row = mysql_fetch_array($result)){
$idFriend = $row['idFriend'];
$array[] = $idFriend;
}
// then implode that array using IN sql statement.
$query_friends = "SELECT * FROM whosout WHERE idPerson IN (implode(','$arr))";
Upvotes: 0
Reputation: 449
You made a small error:
$array = $idFriend . ','; //There should be a period here.
Upvotes: 0