Aiden Ryan
Aiden Ryan

Reputation: 845

A problem with mysql ORDER

I'm trying to order a news section of my website by DESC, so that I can show the most recent posts first, but when i add ORDER BY DESC, i get this error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\DeathRace\site\index.php on line 83

I'm not sure why I'm receiving this error but it is quite unhandy, and if anyone can help me out it would be appreciated, this is the code I am trying to loop:

    $getnews = mysql_query("SELECT * FROM news LIMIT 0, 5 ORDER BY DESC id");
$per_page = 5;
while($row = mysql_fetch_assoc($getnews))
{
    $id = $row['id'];
    $title = $row['title'];
    $body = $row['body'];
    $date = $row['date'];
    $postedby = $row['postedby'];   

    echo 
    "
    $title posted on" .date('d-m-Y' ,strtotime($date))."$body By user:      $postedby <br/>
    ";
}

Upvotes: 0

Views: 179

Answers (3)

Alsemany
Alsemany

Reputation: 445

$sql = "Select field1, field2 from table1 where field1 = 'something' order by field2 desc limit 0,5";

Upvotes: 0

Usman Khalid Qureshi
Usman Khalid Qureshi

Reputation: 146

There is an error in the Query you need to change to query like this

SELECT * 
FROM news 
ORDER BY id DESC 
LIMIT 0 , 5

This will give you the last 5 records from the table

Upvotes: 0

Nicola Cossu
Nicola Cossu

Reputation: 56407

SELECT * FROM news ORDER BY id desc limit 5

edit. If you want to display a mysql date in another format, for example d-m-Y, you can use date_format() function within your query

select field1,field2,....,date_format(your_date,'%d-%m-%Y') as your_date from ...

Upvotes: 4

Related Questions