luke
luke

Reputation: 2773

turning MySQL values into PHP variable when using JOIN

I can get a value from the database and echo it out like so:

$sql = "SELECT id 
          FROM table1";

$result = $conn->query($sql);
$res = mysqli_fetch_assoc($result);
echo $res['id'];

Although, when using any sort of join an issue occurs

$sql = "SELECT table1.id,
               table2.id 
          FROM table1
          JOIN table2";

$result = $conn->query($sql);
$res = mysqli_fetch_assoc($result);
echo $res['id'];

I can't echo out the value like I previously could with a single table.

How do I echo out the id from a specific table, either table1 or table2? I tried echo $res['table1.id']; but that doesn't seem to work out

Upvotes: 0

Views: 41

Answers (1)

Yuvaraj Jeganathan
Yuvaraj Jeganathan

Reputation: 90

$sql = "SELECT table1.id as id,
           table2.id as id1 
      FROM table1
      JOIN table2";
 $result = $conn->query($sql);
 $res = mysqli_fetch_assoc($result);
 echo $res['id1']."--".$res['id'];

Upvotes: 1

Related Questions