Jump to content

Limiting Only Latest 5


googlexx

Recommended Posts

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);

?>

Link to comment
https://forums.phpfreaks.com/topic/160157-limiting-only-latest-5/
Share on other sites

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

    }	
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.