0

I need some help with my code. I have a trouble with connecting to the two different mysql database tables at the same time.

I'm getting an error: Column 'id' in field list is ambiguous IN PHP

Which it have highlight on this line:

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

Here is the code:

$qrytable1="SELECT id, channels, links, categories, streams 
FROM channels_list
INNER JOIN chris_channels ON (chris_channels.channels = channels_list.channels)";
  $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>";
    echo "<p id='streams'>".$row["streams"]. "</p>";
  }

Can you please help me with how I could correct the error to allow me to connect to the mysql tables?

Daniel Ezekiel
  • 133
  • 2
  • 4
  • 11
  • 3
    You have an `id` field in both tables. You need to specify which `id` you need, using something like: `channels_list.id` in your field list (or using table aliases) – ishegg Sep 21 '17 at 00:05
  • Thank you very much for this, I can see the problem is fixed but I want to know how i could connect to two different mysql tables at the same time? – Daniel Ezekiel Sep 21 '17 at 00:10
  • 1
    You are - that's what a `JOIN` does. Have a look at [this](https://stackoverflow.com/a/6188334/8469069) great answer to see how the different types work. – ishegg Sep 21 '17 at 00:17
  • Don't use the `mysql_*` functions. They have been deprecated since v5.5 (Jun 2013) and removed since v7.0 (Dec 2015). Instead use the [**mysqli_***](https://secure.php.net/manual/en/book.mysqli.php) or [**PDO**](https://secure.php.net/manual/en/book.pdo.php) functions with [**prepared statements**](https://secure.php.net/manual/en/pdo.prepare.php) and [**bound parameters**](https://secure.php.net/manual/en/pdostatement.bindparam.php). – Alex Howansky Sep 21 '17 at 00:26

1 Answers1

0

Your entire code is deprecated as mentioned in the comment section above. Do NOT use mysql(). Use mysqli(). I'm even surprised that your IDE did not flag you on that.

Do this

$qrytable1="SELECT cl.id, cl.channels, cl.links, cl.categories, cl.streams 
FROM channels_list cl
LEFT JOIN chris_channels cc ON (cc.channels = cl.channels)";

  $result1 = mysqli_query($db, $qrytable1) 
  or die('Error:<br />' . $qry . '<br />' . mysqli_error());

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

Now replace $db for the database variable you are using to connect your application. I don't know what it is, I just used it as example.

Balloon Fight
  • 661
  • 8
  • 16