0

I need some help with my PHP code. I'm trying to fetch the data from two different tables from the mysql database so I could be able to output each content.

I want to output the contents just like this:

101 BBC One S East

http://www.example.com/bsdev/UK-BBC-1


102 BBC Two

http://www.example.com/bsdev/UK-BBC-2


103 ITV

http://www.example.com/bsdev/UK-ITV-1

Here is what the output show of the contents:

101 BBC One S East

http://www.example.com/bsdev/UK-BBC-1

http://www.example.com/bsdev/UK-BBC-2

http://www.example.com/bsdev/UK-ITV-1


102 BBC Two

http://www.example.com/bsdev/UK-BBC-1

http://www.example.com/bsdev/UK-BBC-2

http://www.example.com/bsdev/UK-ITV-1


103 ITV

http://www.example.com/bsdev/UK-BBC-1

http://www.example.com/bsdev/UK-BBC-2

http://www.example.com/bsdev/UK-ITV-1

Here is the code:

$qrytable1="SELECT id, channels, links, categories FROM channels_list";
  $result1 = mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());

  while ($row = mysql_fetch_array($result1)) 
  {
    echo "<p id='channels'>".$row["id"]. " " . $row["channels"]. "</p>";

    $qrytable2="SELECT id, channels, streams FROM chris_channels";
    $result2 = mysql_query($qrytable2) or die('Error:<br />' . $qry . '<br />' . mysql_error());

    while ($row = mysql_fetch_array($result2)) 
    {
      echo "<p id='streams'>".$row["streams"]. "</p>";
    }
    //mysql_close();
    //exit;
  }
  mysql_close();
  exit;

Can you please show me an example how I could use to get the contents from two different tables of the database to output the contents I want without looping?

Daniel Ezekiel
  • 133
  • 2
  • 4
  • 11
  • Assuming that `id` on `chris_channels` is a foreign key of `id` on `channels_list`, you should be able to do a mysql [join](https://dev.mysql.com/doc/refman/5.7/en/join.html). – Jonnix Sep 17 '17 at 21:38
  • can you please show me an example please? – Daniel Ezekiel Sep 17 '17 at 21:59

1 Answers1

0

You need to use joins between the tables i.e.

$qrytable1="SELECT id, channels, links, categories, streams 
FROM channels_list
INNER JOIN chris_channel ON (chris_channel.channels = channel_list.channels)";

[EDIT]

Both tables have a field called id. You need to define which one you want to display/use/return. I presume it is the one from channels_list so you would need to change the query to:

$qrytable1="SELECT channels_list.id, channels, links, categories, streams 
FROM channels_list
INNER JOIN chris_channel ON (chris_channel.channels = channel_list.channels)";
  • Thank you, but when I try that it will give me an error `Column 'id' in field list is ambiguous`. any idea? – Daniel Ezekiel Sep 17 '17 at 21:59
  • @Daniel perhaps a minimum of absolute basic research on that error message on your part …? -> https://stackoverflow.com/questions/6638520/1052-column-id-in-field-list-is-ambiguous – CBroe Sep 18 '17 at 11:46