I have multiple MYSQL SELECT querys feed in each other, the first one selects the row id, puts them in an array, The second SELECT query needs to use those values 10,14 etc, to acess the rest of the restaurants info in other tables, the problem im having is the second query is only selecting the last phone_number based on the last value in the array 14.
$city= 'london';
$cuisine ='indian';
$sql=(" SELECT restaurant_id FROM restaurants WHERE city = '$city' AND cuisine= '$cuisine' ");
mysql_select_db('RESTAURANTS');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
foreach ($row as $key => $value) {
$i++; // increment $i by one each loop pass
echo "Cart Item $i ______ key = $key | value = $value<br />"; }
$sql1 = "SELECT phone_number FROM restaurants WHERE ($key) IN ($value)";
mysql_select_db('RESTAURANTS');
$retval1 = mysql_query( $sql1, $conn );
if(! $retval1 )
{
die('Could not get data: ' . mysql_error());
}
while($row1 = mysql_fetch_array($retval1, MYSQL_ASSOC))
foreach ($row1 as $k => $v) {
$ii++; // increment $i by one each loop pass
print_r ($v);
}
?>
outputs
Cart Item 1 ______ key = restaurant_id | value = 10
Cart Item 2 ______ key = restaurant_id | value = 14
// the phone_number
444444444
Only selects the phone_number of 14 not 10 also why? do I need some sort of extra function to loop through the array again, iv’e tried implode and mysql_value functions, iv’e tried everything I can think off, any help appreciated.
Your code is attrociously formatted. Had you used proper indenting, you’d see that your first while() loop is malformed:
As such, it will retrieve all the rows of that first query and output the individual fields. But since you didn’t use proper bracketing, the next chunk of code (your next sql query/loop) is NOT part of this first while loop, so you only execute that phone number fetching query ONCE.
It should be