[英]how to fetch specific and unique data from database different tables in php
[英]How to fetch specific column data from the different tables in php/mysql
我正在使用php在教師和學生數據庫上創建一個網站,我在mysql數據庫中幾乎完成了所有工作,但是我想從表中獲取特定數據,就像我想從“名稱”列中選擇“教授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>
這是上面代碼的輸出
您正在尋找mysql連接。
SELECT table1.name, table2.col2
FROM table1
JOIN table2
ON table1.erp = table2.erp
WHERE table1.name = 'Prof. Vaidya Sir';
也就是說,首先將兩個表連接到它們的公共列,然后選擇要在其中查找名稱匹配的特定列。
試試這樣的免責聲明:我通常不使用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);
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.