<?php
require_once 'init.php';
$selectpost = mysql_query("
SELECT
posts.id,
posts.author,
posts.postText
FROM posts
GROUP BY posts.id
");
while($row = mysqli_fetch_object($selectpost)) { //line 30
$posts[] = $row;
}
?>
<?php foreach($posts as $post): ?> //line 37
<div class="post row">
<p><?php echo $post->author; ?> said: </p>
<p><?php echo $post->postText; ?></p>
</div>
<?php endforeach; ?>
我正在尝试显示数据库中的一些帖子。这是我到目前为止编写的代码。出于某种原因,我收到了这些错误:
警告:mysqli_fetch_object()期望参数1为mysqli_result,第30行的posts.php中给出布尔值
注意:未定义的变量:第37行的posts.php中的帖子
警告:第37行的posts.php中为foreach()提供的参数无效
我评论了相应的一行。
答案 0 :(得分:1)
我改变并测试了工作正常,检查一次。在你的代码中没有连接数据库所以记录没有来......
<?php
//require_once 'init.php';
$con=mysql_connect("localhost","root","");
$db=mysql_select_db("test",$con);
$selectpost = mysql_query("
SELECT
posts.id,
posts.author,
posts.postText
FROM posts
GROUP BY posts.id
");
while($row = mysql_fetch_object($selectpost)) { //line 30
$posts[] = $row;
}
?>
<?php print_r($row); foreach($posts as $post): ?>
<div class="post row">
<p><?php echo $post->author; ?> </p>
<p><?php echo $post->postText; ?></p>
</div>
答案 1 :(得分:0)
你正在混合mysql
&amp; mysqli
。如果您使用mysqli_query
,则应为mysql_query
而不是mysqli
。您错过了connection object
-
$selectpost = mysqli_query($conn, "
SELECT
posts.id,
posts.author,
posts.postText
FROM posts
GROUP BY posts.id
");