gpt4 book ai didi

php - 表值未被回显

转载 作者:行者123 更新时间:2023-11-29 00:01:07 27 4
gpt4 key购买 nike

我是 PHP 的新手,正在努力解决一些问题。

该表有“链接”和“评级”列,我从表中随机选择一行。

<?php
$select = mysqli_select_db($conn, "database");
$result2 = mysqli_query($conn, "SELECT * FROM photos ORDER BY rand() LIMIT 1");
?>

如果我添加

while ($row = $result2->fetch_assoc()) { echo $row['rating']; };

$result2 的正下方, 它被正确回显。但这不是我想要的。我后面有一大堆 HTML 代码,还有更多的 php 标签。

它需要放在标题中 <h1>标记是上面编写的代码下方的许多行。

<h1> <?php while ($row = $result2->fetch_assoc()) { echo $row['rating']; }; ?> </h1>

h1 中没有任何回显标签。

最佳答案

您还有其他 SQL 查询 - 您忘记提及了。

将SQL查询的结果保存到变量中,然后只写这个变量。

<?php
$result2 = ...; // SQL query
$row = $result2->fetch_assoc(); // no while loop necessary if you hav ejust one records from database
$rating = $row['rating'];
...
?>
...
<h1><?php echo $rating; ?></h1>

关于php - 表值未被回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789465/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com