0

I have 2 tables

1)students

students table looks like

stu_slno        stu_gr           stu_name
1               1                ABCDESDFDGFJ
2               3                KJJJHJILKJBJB
3               5                HAHAHAHAHKJHKJH
4               1                BBJHBAHJBHJBAJHK

2)groups

groups table looks like

sl_no         pg_groups

1             01-M.A HISTORY
3             03-M.A SOCIOLOGY
5             04-M.A ECONOMICS

i have inserted data into students with groups serial numbers when i am retrieving data from students i will get the groups serial number but what i want is to group names corresponding to the serial number

my code is of retrieving

<?PHP
$sql="SELECT * FROM students";
if ($us=$conn->query($sql)){;
if ($us->num_rows > 0) {
echo '<table border="2">';

    echo "<tr>";

        echo "<th>Slno</th>";

        echo "<th>Name</th>";

        echo "<th>Group</th>";

    echo "</tr>";
while($row = $us->fetch_assoc()) {
    echo "<tr>";

    echo "<td>" .$i++. "</td>";

    echo "<td>" .$row['stu_name']. "</td>";

    echo "<td>" .$row['stu_gr']. "</td>";   
    echo "</table>";
}

}
}

?>
1
  • use joins to get data from two tables. Commented Apr 6, 2016 at 15:04

2 Answers 2

1

use join in query like this:

$sql="SELECT stu_slno, pg_groups, stu_name 
  FROM students s
  INNER JOIN groups g ON g.pg_groups = s.stu_gr";
Sign up to request clarification or add additional context in comments.

Comments

0
$sql = "SELECT s.*, g.pg_groups from students AS s LEFT JOIN groups as g ON g.sl_no = s.stu_gr";

TO display in the HTML table, use the below code

echo "<td>" .$row['pg_groups']. "</td>";  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.