简体   繁体   中英

MySQL / PHP - how to refer to and compare current loop field name from inside nested loop

I'm building a TV Guide. I want to do a single query "select * from programmes". Because, I've had it working with lots of little queries, ie, 1 per each channel, but that's inefficient. Please can somebody help see what is wrong here? I'm getting "Fatal error: Cannot use object of type mysqli_result as array in script.php"

channels table

name
bbcone
bbctwo

programmes table

channel - time - title
bbcone - 6pm - news 
bbcone - 6.30 - weather 
bbcone - 7pm - the talk show 
bbctwo - 6pm - simpsons
bbctwo - 6.30 - futurama
bbctwo - 7pm - nature



$channels=mysqli_query($db,$getchans); 
$programmes=mysqli_query($db,$getprogs);  //holds all programmes

while ($channel = $channels->fetch_assoc())
{
css channel name divs and echo $channel['name']; //works

       while ($programme = $programmes->fetch_assoc()) 
       {
           if ($programme['channel'] == $channel['name']); 
           {    
            do programme divs
           }
       }
}

I can echo both values $programme['channel'] and $channel['name'] elsewhere and they both work, eg, BBC1.

I can't for the life of me work out how to loop through, foreach channel, then loop through the programmes for that channel

(expected output)

bbcone - 6pm news - 6.30 weather - 7pm the talk show - 8pm etc
bbctwo - 6pm simpsons - 6.30 futurama - 7pm nature - 8pm etc 

Please can anyone tell me what I'm doing wrong?

while ($channel = $channels->fetch_assoc())
{
  // $getprogs "select * from programmes where channel_name = $channel["channel_name"]"
  $programmes=mysqli_query($db,$getprogs);
  while ($programme = $programmes->fetch_assoc()) 
   {
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM