Jump to content

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

$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";

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

    }	
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.