我不明白为什么我得到这个错误

时间:2014-11-28 19:42:26

标签: php mysql

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in D:\xampp\htdocs\Kurs\include\news.php on line 32

我收到此错误,我无法理解为什么它是一个简单的代码,不应该有任何错误。这是我的代码:

<?php

$select_news = "SELECT title, content, date_created FROM news ORDERED BY date_created DESC LIMIT 4";
$run_news = mysqli_query($con, $select_news);
while ($news = mysqli_fetch_assoc($run_news)){
$post_title = $news['title'];
$post_content = $news['content'];
$post_date = $news['date_created'];
        ?>
<div id='c'>
<div class='tit'><span class='k'><?php echo $post_title; ?></span></a><text><?php echo $post_date; ?></text></div></br>
<div class='comment more'><?php echo $post_content;?></div></br>
<div id='cherta'>-------------------------------------------------------------------------------------------------------------------------</div>
</div>
<?php
}
?>

我包含了连接到数据库的connect.php文件,这里是我的连接文件的代码:

<?php
$con= mysqli_connect("localhost","root","waterpolo387","kurss");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

?>

2 个答案:

答案 0 :(得分:2)

您的查询失败并因此false而返回。

这是$run_news的内容。您应该检查查询的语法。

提示: 其ORDER BY代替ORDERED BY

$select_news = "SELECT title, content, date_created FROM news ORDER BY date_created DESC LIMIT 4";

答案 1 :(得分:0)

查询失败时出现错误。试试这个:

<?php

$select_news = "SELECT title, content, date_created FROM news ORDERED BY date_created DESC LIMIT 4";
$run_news = mysqli_query($con, $select_news);
echo mysqli_error($con);
while ($news = mysqli_fetch_assoc($run_news)){
$post_title = $news['title'];
$post_content = $news['content'];
$post_date = $news['date_created'];
        ?>
<div id='c'>
<div class='tit'><span class='k'><?php echo $post_title; ?></span></a><text><?php echo $post_date; ?></text></div></br>
<div class='comment more'><?php echo $post_content;?></div></br>
<div id='cherta'>-------------------------------------------------------------------------------------------------------------------------</div>
</div>
<?php
}
?>

然后你会看到你的错误。