我想从数据库的2个表中回显一些数据,但我的第二个表"images"的数据不会显示。
结果:
- 1数据库(没有连接问题,所以很好)
- 2表:
- "文章"、列(id、标题、文本)
- "images",列(idimage, image-title, image-path)
- 我想在一个网页上显示这两个表的数据 在div "content-text"中,我需要显示来自第二个表"images"的一些图像
网页:
<div id="wrapper">
<?php
include_once $_SERVER["DOCUMENT_ROOT"] . "/config.php";
$title = str_replace ('-', ' ', $_GET['title']);
$sql = "SELECT * FROM `articles` WHERE title = '$title'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<div class="content-title">
<?php echo $row['title'];?>
</div>
<div class="content-text">
<?php echo $row['text'];?>
<?php
$imagesql = "SELECT * FROM `images` WHERE image-title = '$title'";
$imageresult = $conn->query($imagesql);
if ($imageresult->num_rows > 0) {
while($imagerow = mysqli_fetch_array($imageresult)) {
?>
<a class="swipebox" href="<?php echo $imagerow['image-path'];?>" title="<?php echo $imagerow['image-title'];?>">
<img alt="image" src="<?php echo $imagerow['image-path'];?>"></a>
<?php
}// end while
}// end if
else {
echo '0 results';
}// end else
?>
</div> //end content-text
<br>
<?php
}// end while
}// end if
else {
echo '0 results';
}// end else
?>
<?php
// close the connection
$conn->close();
?>
</div> // end wrapper
确切的问题:
从表"文章"中返回标题和文本工作正常,但是当我试图从表"图像"中返回数据时,会出现错误,因为它给了我错误"0结果"。PHP和SQL对我来说仍然是新的,我没有设法找到这个解决方案…
MySQL中使用空格、连字符等转义标识符:
SELECT * FROM `images` WHERE `image-title` = '$title'