googlexx Posted May 29, 2009 Share Posted May 29, 2009 I made this code to get 2 things from my database. And it works perfectly except it displays every row. How do i make it just display the last 5 rows added? <?php require_once("functions.php"); require_once("config.php"); $sortcat=$_GET["s"]; $order=$_GET["o"]; $sql = "SELECT COUNT( DISTINCT id ) as totgames from games"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $totgames=$row["totgames"]; } mysql_free_result($result); $sql = "SELECT g.id, map, datetime, gamename, ownername, duration, creatorname, dg.winner, CASE when(gamestate = '17') then 'PRIV' else 'PUB' end as type FROM games as g LEFT JOIN dotagames as dg ON g.id = dg.gameid ORDER BY $sortcat $order, datetime desc"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result, MYSQL_ASSOC) ) { $gamename=$row["gamename"]; $creator=$row["creatorname"]; print $gamename; print $creator; } mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/160157-limiting-only-latest-5/ Share on other sites More sharing options...
gevans Posted May 29, 2009 Share Posted May 29, 2009 $sql = "SELECT g.id, map, datetime, gamename, ownername, duration, creatorname, dg.winner, CASE when(gamestate = '17') then 'PRIV' else 'PUB' end as type FROM games as g LEFT JOIN dotagames as dg ON g.id = dg.gameid ORDER BY $sortcat $order, datetime desc LIMIT 5"; Quote Link to comment https://forums.phpfreaks.com/topic/160157-limiting-only-latest-5/#findComment-844986 Share on other sites More sharing options...
googlexx Posted May 29, 2009 Author Share Posted May 29, 2009 thanks, however i'm getting this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [path]\modules\games.php on line 7 <?php include 'config.php'; $sql = "SELECT gamename, creatorname FROM games LIMIT 5"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $gamename=$row["gamename"]; $creator=$row["creatorname"]; ?> <table width="400" border="1"> <tr> <td><?php print $gamename; ?></td> <td><?php print $creator; ?></td> </tr> </table> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160157-limiting-only-latest-5/#findComment-845039 Share on other sites More sharing options...
Daniel0 Posted May 29, 2009 Share Posted May 29, 2009 Probably some sort of error occurred. Try to run the query manually (using e.g. phpMyAdmin) and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/160157-limiting-only-latest-5/#findComment-845042 Share on other sites More sharing options...
googlexx Posted May 29, 2009 Author Share Posted May 29, 2009 i ran: SELECT gamename, creatorname FROM games LIMIT 5 and it worked good in phpmyadmin Quote Link to comment https://forums.phpfreaks.com/topic/160157-limiting-only-latest-5/#findComment-845044 Share on other sites More sharing options...
Daniel0 Posted May 29, 2009 Share Posted May 29, 2009 Try to change $result = mysql_query($sql); to $result = mysql_query($sql) or trigger_error(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/160157-limiting-only-latest-5/#findComment-845047 Share on other sites More sharing options...
googlexx Posted May 29, 2009 Author Share Posted May 29, 2009 hm, it seems to be working when i go to the page but i was trying to add this as a vbulletin module and i only get the error when its in the module..... btw: how do i query it to get the latest and not the first 5? Quote Link to comment https://forums.phpfreaks.com/topic/160157-limiting-only-latest-5/#findComment-845051 Share on other sites More sharing options...
Daniel0 Posted May 29, 2009 Share Posted May 29, 2009 btw: how do i query it to get the latest and not the first 5? Change LIMIT 5 to LIMIT 5 DESC in your query. Quote Link to comment https://forums.phpfreaks.com/topic/160157-limiting-only-latest-5/#findComment-845052 Share on other sites More sharing options...
googlexx Posted May 29, 2009 Author Share Posted May 29, 2009 hm now thats weird. when i add DESC to the end i get the error but when i remove it, then it works... Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\forums\modules\games.php on line 7 Quote Link to comment https://forums.phpfreaks.com/topic/160157-limiting-only-latest-5/#findComment-845057 Share on other sites More sharing options...
Daniel0 Posted May 29, 2009 Share Posted May 29, 2009 Sorry, my bad. It has to be like ORDER BY someField DESC, not just DESC. Quote Link to comment https://forums.phpfreaks.com/topic/160157-limiting-only-latest-5/#findComment-845060 Share on other sites More sharing options...
googlexx Posted May 29, 2009 Author Share Posted May 29, 2009 what do i put if i want it ordered by earliest to latest? Quote Link to comment https://forums.phpfreaks.com/topic/160157-limiting-only-latest-5/#findComment-845062 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.