Echoing entries from MySQL

tnl2k7

Banned
Messages
3,131
Reaction score
0
Points
0
Right then here goes,

I've got a MySQL database running as the backend for my arcade. It contains one table, named 'games' with two columns, one called 'name' and one called 'type'. The 'name' column is the primary key. All the data for the arcades game names and locations are stored within this table.

Example, a game called Alien Abduction in Shockwave format would have an entry in the name column as 'Alien-Abduction' and in the type column 'dcr'. The page it would be embedded on would be 'http://www.productivityzero.uk.to/play.php?id=Alien-Abduction'.

Now though, I'm a little bit annoyed. I want to echo the 5 latest games uploaded to the site. But because of the lack of a time / date column I can't see a way of doing it. Everything in the MySQL table seems to be in the order I uploaded it, with the latest games being shown at the bottom of the records list in phpMyAdmin.......if that helps.

The code I'm using to show all the games is this:

Code:
<?php
include("shared/script/mysql-connect.php");
$result = mysql_query("SELECT * FROM games ORDER by name ASC") or die(mysql_error());
echo("<h3>content...</h3>");
while($row = mysql_fetch_array( $result )) {
$link = str_replace("-"," ",$row['name']);
echo "<p><a href=\"play.php?id=";
echo $row['name'];
echo "\">";
echo $link;
echo "</a></p>";
}
?>

So I'm hoping to be able to use something similar in order to perform this task.

All the pages are automatically generated, I don't really want this one to be an exception. Any ideas, experts?

If you need any more information, just ask and I'll update this post.

Thanks,
Luke.
 
Last edited:

Anna

I am just me
Staff member
Messages
11,758
Reaction score
586
Points
113
this should do the trick, just replace the qeury:
Code:
$result = mysql_query("SELECT * FROM games ORDER by name desc LIMIT 5") or die(mysql_error());
 

tnl2k7

Banned
Messages
3,131
Reaction score
0
Points
0
LadyAnna,

Thanks for your help.

The problem is, wouldn't this code only display the last entries alphabetically, as opposed to the most recently added? I need the ones entered into the database most recently. The table, as I said above, shows the latest games at the bottom, oldest at the top, NOT in alphabetical order. I was wondering if there was a way to show only the bottom five...

I think I'll have to delete my database and start again using a unique number for each game added, the first game being 1, the second 2, and so on.

Before I do this, does anyone else have any ideas?

Luke.

EDIT: I've created a new database and am manually transferring the 40 or so entries into it using the administration panel I sat and wrote a couple of weeks back. I knew it'd be useful. I've added a new primary key column using AUTO_INCREMENT that seems to do the job. Heck, I wish I'd done this originally.....
 
Last edited:

cowctcat

New Member
Messages
401
Reaction score
0
Points
0
if you want to show only the bottom five add:
Code:
ORDER BY id DESC LIMIT 5
to your query, assuming that your auto increment column is called id
 
Last edited:
Top