nogggin1
nogggin1

Reputation: 13

Display only latest three results from PHP and MySQL

<?php

$result = @mysql_query('SELECT Article FROM news WHERE ID = (SELECT MAX(ID) FROM News)');
if (!$result) {
    die('<p>Error performing query: ' . mysql_error() . '</p>');
}
while ($row = mysql_fetch_array($result)) {
    echo('<p>' . $row['Article'] . '</p>');
}

?>

Basically, I need to tweak this code, so that it shows the latest three results instead of just the latest one, the newest being the first.

Upvotes: 0

Views: 4744

Answers (3)

Xavier Barbosa
Xavier Barbosa

Reputation: 3947

this query ?

SELECT Article FROM News ORDER BY ID DESC LIMIT 3

Upvotes: 1

lonesomeday
lonesomeday

Reputation: 237845

Have a go with this query:

$result = @mysql_query('SELECT Article FROM news ORDER BY ID DESC LIMIT 3');

FWIW, you may like to sort out the capitalisation of your database columns. The inconsistency will cause you problems in future.

Upvotes: 1

Edmhs
Edmhs

Reputation: 3731

in mysql query add limit

select * from table order by id desc limit 0,3

Upvotes: 5

Related Questions