Home > other >  Joining three tables and displaying value using php
Joining three tables and displaying value using php

Time:06-20

I have used join and was able to get output table in phpmyadmin using this query :

$query    =    "SELECT members.usn, members.name, events.ename FROM members JOIN participant ON members.usn = participant.usn JOIN events ON events.eid = participant.eid WHERE members.usn='.$usn.'";
$result    =    $conn->query($query);

The Output was

usn name ename
7DC18CS005 John Robo Wars

I used PHP to display this in my homepage for specific user by maintaining usn in their SESSION.

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo '<tr>';
        echo '<td>';
        echo  $row[0];
        echo '</td>';
        echo '<td>';
        echo  $row[1];
        echo '</td>';
        echo '<td>';
        echo  $row[2];
        echo '</td>';
        echo '</tr>';
    }
} else {
    echo "No Members[Connected DB]";
}

But the output gives me "No Members[Connected DB]" instead of expected output which is

usn name ename
7DC18CS005 John Robo Wars

Thank you!

CodePudding user response:

You have a typo in the query. Looks like you are trying to concatenate $usn to the query but you got it wrong.

Change

$query = "SELECT members.usn, members.name, events.ename FROM members JOIN participant ON members.usn = participant.usn JOIN events ON events.eid = participant.eid WHERE members.usn='.$usn.'";

To

$query = "SELECT members.usn, members.name, events.ename FROM members JOIN participant ON members.usn = participant.usn JOIN events ON events.eid = participant.eid WHERE members.usn='$usn'";
  • Related