Noob's ignorance: Just basic php code question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Forum Newbie
Posts: 5
Joined: Wed Jun 07, 2006 1:49 pm

Noob's ignorance: Just basic php code question

Post by EÜRENN »

In an attempt to learn mysql database and flash integration, I've started putting together a little flash app that reads and submits a "blog" entry.

Right now it does two things, 1 it reads the fields "title" and "content" in the db table blogTable. So far i only have 2 rows of data, my first entry (0), and the latest entry (1).

What i would like to know is how to display the latest entry in the table instead of the first (0).

I know that below in the code, if i replaced 0 with 1, it would show the latest entry, but of course that's not what i should have because once i submit a new entry in the table, the latest row would be 2.

Code: Select all

$query = "SELECT * FROM blogTable";
$result = mysql_query($query);

$title = mysql_result($result,0,"title");
$content = mysql_result($result,0,"content");
So basically i need to figure out how to make this list the latest entry in the table.

Any of your guys' help on this is so much appreciated.

Thanks! :)
User avatar
Forum Commoner
Posts: 34
Joined: Sat Jun 10, 2006 7:06 am
Location: Netherlands

Post by ronverdonk »

Well, it is not so much a PHP question as an SQL question. You must have something that you can use to order your rows. E.g. if you are using an auto_increment key or a date of insertion of your table rows, you can use that by simply sorting on that column (let's call it your_sortable_field) DESCENDING, so the last one is always the first to retrieve.

In such a case simple sort your query result and make your statement:

Code: Select all

$query = "SELECT * FROM blogTable ORDER BY your_sortable_field DESC";
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)

Post by MarK (CZ) »

Also using 'LIMIT 1' when using just the first row of the query makes the query faster (especially in case of large tables).
Same goes for using columns list insted of *.
Post Reply