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

I get this error and i cant understand why its a simple code and there shouldn't be any error. Here is my code:

<?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
}
?>

I included and the connect.php file who connects me to the DB and here is the code of my connect file:

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

?>
lfc123
  • 141
  • 8

2 Answers2

2

Your query failed and returns because of this false.

This is your content of $run_news. You should check your syntax of your query.

Hint: its ORDER BY instead of ORDERED BY.

$select_news = "SELECT title, content, date_created FROM news ORDER BY date_created DESC LIMIT 4";
tjati
  • 5,453
  • 3
  • 35
  • 54
0

You get that error when query failed. Try this:

<?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
}
?>

Then you will see your error.

Efog
  • 1,152
  • 14
  • 33