简体   繁体   中英

Echo data from two different tables in database: echo data 2nd table not working

I want to echo some data from 2 tables of my database but the data of my second table "images" won't show up.

What I got:

  • 1 database (no connection problem so that's fine)
  • 2 tables:
    • "articles" , columns (id, title, text)
    • "images" , columns (idimage, image-title, image-path)
  • I want to display data from those two tables on one webpage
  • within the div "content-text", I need to display some images from the second table "images"

The webpage:

<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

The exact problem:

Echo the title and text from the table "articles" works fine, but there's something wrong when I try to echo the data from the table "images" cause it gives me the error "0 results". PHP and SQL is still new to me and I didn't manage to find the solution for this...

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

SELECT * FROM `images` WHERE `image-title` = '$title'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM