如何通过php将mysql中的这些数据插入到我的网页中?

时间:2018-06-21 15:50:32

标签: php mysql

am尝试使用多个关联行的数据库中的数据将它们提取到ROW1,ROW2和ROW3中,但使用

<?php echo $row['']; ?>

但是它不起作用,请提出任何想法

这是我的代码

<?php
$sql = "SELECT * FROM songs ORDER BY id DESC LIMIT 2,1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {

}
?>

<li>
<a href="single.html"><img src="images/<?php echo $row['photo']; ?>" alt=""/> 
</a>
<div class="slide-title"><h4><?php echo $row['song_name']; ?></div>
<div class="slide-title"><h4><?php echo $row['artist']; ?></div>
<div button class="btn btn-large btn-primary" type="button">BUY</div>
</li>

2 个答案:

答案 0 :(得分:0)

在回显结果之前,您似乎正在关闭while循环。只需在结束}之后移动结束括号</li>

您的代码应如下所示:

<?php
$sql = "SELECT * FROM songs ORDER BY id DESC LIMIT 2,1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);

if ($resultCheck > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
?>
<li>
<a href="single.html"><img src="images/<?php echo $row['photo']; ?>" alt=""/> 
</a>
<div class="slide-title"><h4><?php echo $row['song_name']; ?></div>
<div class="slide-title"><h4><?php echo $row['artist']; ?></div>
<div button class="btn btn-large btn-primary" type="button">BUY</div>
</li>
<?php
    }
}
?>

答案 1 :(得分:0)

尝试此代码

<?php
$sql = "SELECT * FROM songs ORDER BY id DESC LIMIT 2,1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);

if ($resultCheck > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        foreach ($row as $data) {
?>
<li>
<a href="single.html"><img src="images/<?php echo $row['photo']; ?>" alt=""/> 
</a>
<div class="slide-title"><h4><?php echo $data['song_name']; ?></div>
<div class="slide-title"><h4><?php echo $data['artist']; ?></div>
<div button class="btn btn-large btn-primary" type="button">BUY</div>
</li>
<?php
        }
    }
}
?>