如何从php / mysql

时间:2017-02-24 18:26:58

标签: php mysql mysqli

我在php中创建了一个关于教师和学生数据库的网站,我在mysql数据库中做了几乎所有的事情,但我想从表中获取特定数据,比如我想从列中选择“prof.vaidya先生” “名称”和显示教授相关的所有数据。

这是我的代码:

<!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

2 个答案:

答案 0 :(得分:0)

您正在寻找mysql连接。

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

这表示首先将两个表连接到它们的公共列,然后选择您要查找的名称匹配的特定列。

答案 1 :(得分:0)

尝试这样的事情 免责声明:我通常不使用mysqli,所以我认为我没错。

你应该使用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);
?>