-1

i create 2 tables, student and class. and also i used stud_id as a foreign key in class table.... how i get both tables values using stud_id???? also i am using stud_id in session....

{<?php

 $id=$_SESSION["Stud_id"];

 $query = mysql_query ("SELECT * FROM student where Stud_id='$id'");

 if (!$query)
 {
 die('Invalid query: ' . mysql_error());
 }
 while($row = mysql_fetch_assoc($query)) {

    echo "<tr><td> Student ID:</td><td> " . $row["Stud_id"]. "</td></tr>";
    echo "<tr><td> Student Name:</td><td> " . $row["Stud_name"]. "</td>  </tr>";
    echo "<tr><td> Password:</td><td> " . $row["password"]. "</td></tr>";

    echo "<tr><td> Father:</td><td> " . $row["Stud_father"]. "</td></tr>";
    echo "<tr><td> Date Of Birth:</td><td> " . $row["Stud_dob"]. "</td>     </tr>";
    echo "<tr><td> Address:</td><td> " . $row["Stud_address"]. "</td></tr>";
    echo "<tr><td> Gender:</td><td> " . $row["Stud_gender"]. "</td></tr>";
    echo "<tr><td> Phone No:</td><td> " . $row["Stud_phone"]. "</td></tr>";
    echo "<tr><td> Class</td><td> " . $row["Stud_class"]. "</td></tr>";


}?>
furqan
  • 1
  • 1
  • 1
    http://dev.mysql.com/doc/refman/5.7/en/join.html – Madhawa Priyashantha Jun 30 '16 at 16:14
  • Possible duplicate of [How can an SQL query return data from multiple tables](http://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables) – showdev Jun 30 '16 at 16:14
  • **WARNING**: If you're just learning PHP, please, do not use the [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) explains best practices. Your user parameters are **not** [properly escaped](http://bobby-tables.com/php) and there are [SQL injection bugs](http://bobby-tables.com/) that can be exploited. – tadman Jun 30 '16 at 16:46

2 Answers2

3

Join the tables. So instead of this structure:

SELECT *
FROM student
WHERE ...

You'd have this structure:

SELECT *
FROM student
INNER JOIN class ON student.stud_id = class.stud_id
WHERE ...

This would give you the product of both tables, with all columns from both.

Note that I explicitly left out the WHERE clause only so as to not include a SQL injection vulnerability in an answer. You're definitely going to want to look into mysqli and PDO and their use of prepared statements with query parameters. (It's simpler than it sounds.) Currently your code is open to the most common vulnerability on the internet.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
1

You can use the below query to get the results

{<?php

 $id=$_SESSION["Stud_id"];

 $query = mysql_query ("select s.*,c.* from student s join class c on s.Stud_id=c.Stud_id where s.Stud_id = '$id'");

 if (!$query)
 {
 die('Invalid query: ' . mysql_error());
 }
 while($row = mysql_fetch_assoc($query)) {

    echo "<tr><td> Student ID:</td><td> " . $row["s.Stud_id"]. "</td></tr>";
    echo "<tr><td> Student Name:</td><td> " . $row["s.Stud_name"]. "</td>  </tr>";
    echo "<tr><td> Password:</td><td> " . $row["s.password"]. "</td></tr>";

    echo "<tr><td> Father:</td><td> " . $row["s.Stud_father"]. "</td></tr>";
    echo "<tr><td> Date Of Birth:</td><td> " . $row["s.Stud_dob"]. "</td>     </tr>";
    echo "<tr><td> Address:</td><td> " . $row["s.Stud_address"]. "</td></tr>";
    echo "<tr><td> Gender:</td><td> " . $row["s.Stud_gender"]. "</td></tr>";
    echo "<tr><td> Phone No:</td><td> " . $row["s.Stud_phone"]. "</td></tr>";
    echo "<tr><td> Class</td><td> " . $row["s.Stud_class"]. "</td></tr>";


}?>

edit you may have to change the column names above

Andrews B Anthony
  • 1,381
  • 9
  • 27
  • That will effectively perform a cross join, which doesn't seem desirable. I think your query could use an `ON` statement. See [Can I use mysql JOIN without ON condition?](http://stackoverflow.com/questions/16470942/can-i-use-mysql-join-without-on-condition). – showdev Jun 30 '16 at 16:21