我有3个表:为什么我的3表JOIN MySQL查询不起作用?
Table: album
Columns: id, name, description, author, path, image
Table: albumconnect
Columns: id, imageid, albumid
Table: albumimages
Columns: id, path
而且我想用一个单一的连接查询,以取代所有这些不必要的查询:
<?php
$albumID = $_SERVER['QUERY_STRING'];
$realAlbumID = substr($albumID, 1);
$realestAlbumID = str_replace('%20', ' ', $realAlbumID);
$sql = "SELECT * FROM album WHERE id='$realestAlbumID'";
$result = mysqli_query($conn, $sql);
$getResult = mysqli_fetch_assoc($result);
$albumPath = $getResult['path'];
$sql2 = "SELECT * FROM albumconnect WHERE albumid='$realestAlbumID'";
$result2 = mysqli_query($conn, $sql2);
while ($row = $result2->fetch_assoc()){
$imageId = $row['imageid'];
$sql3 = "SELECT * FROM albumimages WHERE id='$imageId'";
$result3 = mysqli_query($conn, $sql3);
$getResult3 = mysqli_fetch_assoc($result3);
$imagePath = $getResult3['path'];
echo '<div class="imageContainerAlbums"><li class="listAlbums"><img class="specificAlbumThumnails" src="'.$albumPath.$imagePath.'" alt="Random image" /></li></div>';
};
?>
现在JOIN查询我想出来的基础上东西我在网上看了是这样的:
$sql5 = "SELECT * FROM album
JOIN albumconnect ON albumconnect.albumid=album.id
JOIN albumimages ON albumimages.id=albumconnect.imageid
WHERE id='$realestAlbumID'";
$result5 = mysqli_query($conn, $sql5);
然而,当我尝试的var_dump内容,它打印空,所以我认为我的查询是不正确,但我想不通的C正确的方式来做到这一点。
你是否直接用PHPmyAdmin或别的东西组成变量来试图查询? – Difster
是的,我已经在我的数据库中有值。 –
我的意思是,直接用SQL命令运行查询,看看你的连接是否有效。 – Difster