简体   繁体   中英

How to fetch specific column data from the different tables in php/mysql

I'm creating a website on teacher and student database in php, i have done almost all things in mysql database but i want to fetch specific data from the table, like i want to choose "prof. vaidya sir" from column "name" and display professor related all data.

Here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php   
include 'dbh.php';
$name = "Prof. Vaidya Sir";
$class ="BE";
$sqll = "SELECT feedback1.erp,feedback1.class,feedback2.name,feedback2.ability,feedback2.knowledge,feedback2.accessbility,feedback2.attitude FROM feedback1, feedback2 WHERE feedback1.erp=feedback2.erp";
$result = $conn->query($sqll);
?>
<table border="2">
  <thead>
    <tr><th>erp</th><th>class</th><th>name</th><th>ability</th><th>knowledge</th><th>accessbility</th><th>attitude</th></tr>
  </thead>
  <tbody>
    <?php
      if($result->num_rows > 0)
         {
            while ($row = $result->fetch_assoc()){

             echo "<tr><td>{$row["erp"]}</td><td>{$row["class"]}</td><td>{$row["name"]}</td><td>{$row["ability"]}</td><td>{$row["knowledge"]}</td><td>{$row["accessbility"]}</td><td>{$row["attitude"]}</td></tr>\n";

        }   
        else{echo "0 results";
        }
?>
 </tbody>
</table>
</body>
</html>

This is output of above code

这是上面代码的输出

You're looking for mysql joins.

SELECT table1.name, table2.col2 
FROM table1
JOIN table2 
ON table1.erp = table2.erp
WHERE table1.name = 'Prof. Vaidya Sir';

This says to first join the two tables on their common columns, then select the specific columns you are looking for where the name matches.

Try something like this disclaimer: I don't usually use mysqli so I think I have it right

you should use JOINS instead of old way it makes the connection clearer and more human readable

<?php 
include 'dbh.php';
$name = "Prof. Vaidya Sir";
$class ="BE";
$sqll = $mysqli->prepare("SELECT feedback1.erp,feedback1.class,feedback2.name,feedback2.ability,feedback2.knowledge,feedback2.accessbility,feedback2.attitude 
         FROM feedback1 join feedback2 ON  feedback1.erp=feedback2.erp
         WHERE feedback2.name =?";

$sqll->bind_param('s', $name);
$sqll->execute();

$result = $sqll->get_result();
var_dump($result);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM