How to put array rows in a table?

I need to add the last three rows to the array from the database table, how can I do this?

include('connections/trooble.php');
$q = mysql_query("SELECT * FROM test ORDER BY (`id`) DESC LIMIT 3");
$r = mysql_fetch_array($q);
while ($r = mysql_fetch_array($q)) {
    $n[] = $r['news'];
}
$i = 0;
while ($i < 3) {
    $a = $n[$i];
    $i++;
    echo $a;
}

Everything seems to be correct, but it does not display the very first news.


Answer 1, authority 100%

If I understand correctly:

$query = mysql_query("SELECT * FROM `table` ORDER BY (`id`) DESC LIMIT 3");
while($result = mysql_fetch_array($query))
{
    //Result of the array
}

Answer 2

$r = mysql_fetch_array($q);
while ($r = mysql_fetch_array($q)) {
    $n[] = $r['news'];
}

here, in my opinion, the first line is superfluous….
it turns out so that fetch is done twice in a row, but after the first one it is not written to the array.
Sorry for my bad english.

Leave a reply

Please enter your comment!
Please enter your name here