2 回答
TA贡献1851条经验 获得超4个赞
您正在返回一个值。如果我必须出示所有用户卡,我会这样做。
<?php
require "connection.php";
$search = $_POST['search'];
$sql = "SELECT users.first_name, users.second_name, cards.card_number
FROM users
INNER JOIN cards
ON cards.user_id = users.id
WHERE
users.first_name = '$search'
OR users.second_name = '$search'";
$query = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($query)) {
echo $row['first_name']." ".$row['second_name']." ",
$row['card_number'];
}
?>
这应该为每张卡打印一行。
您还可以更改类似这样的 where 子句来查找部分字符串:
users.first_name LIKE '%$search%'
但是,如果您希望能够搜索卡号并找到其所有者的所有关联卡,那么您必须采取一些额外的步骤。
我会去做这样的事情:
SELECT t1.customerIDi, cards.number, customer.*
FROM CARDS
LEFT JOIN customer ON cards.CustomerID = customer.id
INNER JOIN (
SELECT customer.id AS customerIDi
FROM customer
LEFT JOIN cards
ON cards.CustomerID = customer.id
WHERE
Cards.Number = "$search"
) AS t1
ON t1.customerIDi = cards.CustomerID
我嵌套了一个选择来查找卡所有者,然后获取所有关联的卡。
TA贡献1874条经验 获得超12个赞
跑步
$results = mysqli_fetch_assoc($query);
foreach( $results as $result) {
echo .....
}
- 2 回答
- 0 关注
- 100 浏览
添加回答
举报