简体   繁体   English

如何从php / mysql中的不同表中获取特定的列数据

[英]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. 我正在使用php在教师和学生数据库上创建一个网站,我在mysql数据库中几乎完成了所有工作,但是我想从表中获取特定数据,就像我想从“名称”列中选择“教授vaidya先生”与展示教授相关的所有数据。

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. 您正在寻找mysql连接。

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 试试这样的免责声明:我通常不使用mysqli,所以我认为我做对了

you should use JOINS instead of old way it makes the connection clearer and more human readable 您应该使用JOINS而不是旧方法,它会使连接更清晰,更易读

<?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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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