[英]Display the name instead of the ID
數據庫設置:
表:客戶
id | name | address | zipcode | city | phone | email | active
表:待辦事項
id | customerid | description | information | active
$sql = "
SELECT *
FROM todo
ORDER
BY `customerid` ASC
, `description` ASC
";
顯示結果:
echo $row['customerid'] $row['description'] $row['information'];
輸出:
customerid description information
所需的輸出:
customername (from table customers) description information
我一直在閱讀這個論壇,我發現我應該使用INNER JOIN,但我無法使其正常工作。
有人可以幫我嗎?
首先,您需要使用join
來獲取客戶名值
SELECT t.description,t.information,c.name
FROM todo t JOIN customers c ON c.id=t.customerid
ORDER BY `t.customerid` ASC, `description` ASC
然后嘗試以下方法:
echo $row['name'] $row['description'] $row['information'];
問題是,當您使用“ ORDER BY”時,不能使用“ *”。 這是代碼的運行方式:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT customers.customerid,name, description, information FROM customers INNER JOIN todo ON customers.customerid=todo.customerid ORDER
BY customers.customerid ASC
, description ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br>". $row["name"]." ".$row["description"]. " " . $row["information"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
這應該做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.