如何在html标签中显示从数据库提取的数据?

时间:2020-08-16 09:08:42

标签: html

我有此php代码可从数据库中获取数据,但似乎无法弄清楚该怎么做,我需要您的帮助:

$sql = "SELECT * FROM moviesdet";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo '<a href="./invideo.php?movieID=' . $row["movieId"] . '">' . $row["movieIMG"] . '</a>';
    }
} else {
    echo "0 results";
}

这个html代码是我要将提取的数据放入的地方:

<div class="someMovies"></div><div class="someMoviesHere"></div>

我想要的是在第一div [(someMovies)中显示数据库中的一些数据,如2-3个元素],在第二个div(someMoviesHere)中显示其他数据,例如数据库中的4-5个元素。

1 个答案:

答案 0 :(得分:-4)

您可以复制并粘贴以下内容,并根据需要修改一些数字。

<div class="someMovies">
    <?php
            $sql = "SELECT * FROM moviesdet LIMIT 3";//you can change number of limit based on your requirement
            $result = $conn->query($sql);
            if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    echo '<a href="./invideo.php?movieID=' . $row["movieId"] . '"> <img height="200px" width="200px" src="'. $row["movieIMG"] . '"></a>';//you can modify height and width of the image to your required one
                }
            } else {
                echo "0 results";
            }
            mysqli_free_result($result);
            unset($sql);
        ?>
        </div>
        <div class="someMoviesHere">
        <?php
            $sql = "SELECT * FROM moviesdet LIMIT 5";//you can change number of limit based on your requirement
            $result = $conn->query($sql);
            if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    echo '<a href="./invideo.php?movieID=' . $row["movieId"] . '"> <img height="200px" width="200px" src="'. $row["movieIMG"] . '"></a>';//you can modify height and width of the image to your required one
                }
            } else {
                echo "0 results";
            }
            mysqli_free_result($result);
            unset($sql);
        ?>
</div>