从数据库中的两个不同表中回显数据:echo数据第二个表不起作用

时间:2014-06-18 18:05:44

标签: php mysql sql database

我想从我的数据库的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

确切的问题:

从表“文章”中回显标题和文本工作正常,但是当我尝试回显表“images”中的数据时出现了错误,因为它给出了错误“0结果”。 PHP和SQL对我来说仍然是新的,我无法找到解决方案......

1 个答案:

答案 0 :(得分:3)

在MySQL中使用带反引号的空格,连字符(等)转义标识符:

SELECT * FROM `images` WHERE `image-title` = '$title'
相关问题