开发者

php, mySQL return last x rows

开发者 https://www.devze.com 2023-01-24 20:26 出处:网络
I\'m working on a php/mySQL/ajax shoutbox and have run into a small snag. On the page I want the content to be loaded from oldest to newest, with the newest on the bottom.I also want to limit the outp

I'm working on a php/mySQL/ajax shoutbox and have run into a small snag. On the page I want the content to be loaded from oldest to newest, with the newest on the bottom. I also want to limit the output to reduce load times once the database starts to get a lot of data in it.

Here is the current code;

    <?php

include_once("../includes/db.php");
include_once("../includes/functions.php");

$q="SELECT tM.*, tC.char_name as username, tC.char_id as char_id
FROM shoutbox tM JOIN characters tC
ON tC.char_id=tM.char_id
ORDER BY shout_id DESC LIMIT 25";

db_Connect();

$result=mysql_query($q);

while($row=mysql_fetch_array($result))
{
    $classColor = getClassColor($row['char_id']);
    echo "<span class='".$classColor."'>".$row['username']."</span>: ",nl2br($row['shout_message'])."<br />";
}

mysql_Close();

?>

I have tried using while($row=array_reverse(mysql_fetch_array($result))) as well as $result = array_reverse(mysql_query($q)) but both return an error that array_reverse needs to be fed an array.

So far anything I have found on the web from the SQL side have all been answered "just use DES开发者_如何学CC or ASC accordingly."


$res = mysql_query($q);
$shouts = array();

while($row = mysql_fetch_assoc($res))
  $shouts[] = $row;
$shouts = array_reverse($shouts);

foreach($shouts as $shout) {
  // show them...
}


If you want oldest to newest, and shout_id is auto increment just use

ORDER BY shout_id ASC LIMIT 25

You have DESC/ASC mixed up


$result=mysql_query($q);

unset($temp_array);

while($row = mysql_fetch_array($result))
    $temp_array []= $row;

$temp_array = array_reverse($temp_array);

foreach ($temp_array as $row)
{
    $classColor = getClassColor($row['char_id']);
    echo "<span class='".$classColor."'>".$row['username']."</span>: ",nl2br($row['shout_message'])."<br />";
}


Seems like a lit of work. I just used this:

$result = mysql_query($strQuery) or die(mysql_error());

while ($row = array_reverse(mysql_fetch_array($result))) {

$row['something'];

}
0

精彩评论

暂无评论...
验证码 换一张
取 消