我有以下代码。此代码未显示任何结果。请说明问题是什么?
<?php
require_once("connnection.php");
$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<div style="position:absolute;top:200px;right:65px; padding:10px; background-color:#CCFFFF; width:300px; height:400px;">
<hr>
<p><strong>Latest posts</strong></p>
<hr style="position:relative; top:40px; left:30px; width:80%; ">
<?php
$sql = "SELECT question FROM comment ORDER BY id DESC LIMIT 5";
if ($result = mysqli_query($con, $sql)) {
/* fetch associative array */
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[3]);
}
/* free result set */
}
?>
我想从数据库中获取五个最新问题并显示它。数据库有五个记录。表注释有4个字段。“问题”是最后一个领域。
$row[3]
未定义。您需要使用$row[0]
while ($row = mysqli_fetch_row($result)) {
printf( "%s\n", $row[0] );
}
或者:
while ($row = mysqli_fetch_assoc($result)) { // notice the use of `fetch_assoc`
echo $row['question'] . "\n";
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句