我想获取具有相同照片ID的表中一行的所有值
<?php
$message = "";
if(empty($_GET['id'])) {
$session->message("<div class='btn sm-warning'>please select an image.</div>");
redirect_to('list_properties.php');
}
$id = $_GET['id'];
$sql = "SELECT * FROM pictures GROUP BY photograph_id HAVING '$id';";
$property = $database->query($sql);
$photos = $database->fetch_array($property);
echo $photos;
?>
这是我下面的sql表...我想使用ID号选择所有具有相同photo_id的图像。
id photograph_id image title location price type
1 etsq1mro6n building-1.jpg 5 Bedroom Lekki 90,000,000 Sale
2 etsq1mro6n building-2.jpg 5 Bedroom Lekki 90,000,000 Sale
3 etsq1mro6n building-3.jpg 5 Bedroom Lekki 90,000,000 Sale
4 etsq1mro6n building-4.jpg 5 Bedroom Lekki 90,000,000 Sale
5 etsq1mro6n building-5.jpg 5 Bedroom Lekki 90,000,000 Sale
6 etsq1mro6n building-6.jpg 5 Bedroom Lekki 90,000,000 Sale
答案 0 :(得分:1)
除了@scaisEdge所说的以外,您当前正在使用HAVING
,这通常在聚合数据时使用-您没有这样做。这是HAVING
查找重复记录的示例,与COUNT()
结合使用:
SELECT
name, email, COUNT(*)
FROM
users
GROUP BY
name, email
HAVING
COUNT(*) > 1
如前所述,您需要使用WHERE
而不是HAVING
:
<?php
$message = "";
if(empty($_GET['id'])) {
$session->message("<div class='btn sm-warning'>please select an image.</div>");
redirect_to('list_properties.php');
}
$id = $_GET['id'];
$sql = "SELECT * FROM pictures WHERE photograph_id = '$id';";
$property = $database->query($sql);
$photos = $database->fetch_array($property);
echo $photos;
?>
作为最佳实践,您还应该使用准备好的语句-https://www.php.net/manual/en/pdo.prepared-statements.php。
答案 1 :(得分:0)
您是否正在寻找基于何处的过滤器
"SELECT * FROM pictures
WHERE photograph_id = '$id';";
例如
"SELECT * FROM pictures
WHERE photograph_id = 'etsq1mro6n';";
无论如何,您都不应该在sql中使用php var,因为您有遭受sqlinjection的风险..您应该查看准备好的语句和bindig参数..