使用php在html中显示数据库中的多条记录

时间:2017-11-20 18:51:40

标签: php html mysql database

我使用php从数据库返回记录,但我希望这些记录显示在不同的HTML段落块中 目前我得到结果的回声,但我想以某种方式在网站上的不同位置显示它们,我怎么能用PHP和HTML做到这一点? 这是我的代码:

php:$ conn是数据库连接

$sql2 = "SELECT * FROM (SELECT * FROM reviews ORDER BY id_review DESC LIMIT 3 ) as result ORDER BY id_review";
$result2 = mysqli_query($conn, $sql2);

if (mysqli_num_rows($result2) > 0) {
    // output data of each row
    while($row2 = mysqli_fetch_assoc($result2)) {
        echo "id: " . $row2["id_review"]. " - Name: " . $row2["content"]. " " . $row2["users_id_user"]. "<br>";
    }
} else {
    echo "0 results";
}

我想要的是:

<p><?php echo $row[row1];?></p> // instead of row1 i want first record of my select and so on
<p><?php echo $row[row2];?></p>
<p><?php echo $row[row3];?></p>
<p><?php echo $row[row4];?></p>

有数组:

$num = mysqli_num_rows($result2);
for ($i = 0; $i < $num; $i++)
{
    $myArray[] = mysqli_fetch_assoc($result2);
}
print_r($myArray[0]['id_review']);
echo '<br>' ;
print_r($myArray[0]['content']);
echo '<br>' ;
print_r($myArray[0]['users_id_user']);
echo '<br>' ;

0 个答案:

没有答案