Selecting most recent entry from db

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
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

Selecting most recent entry from db

Post by dru_nasty »

I'm trying to learn the building blocks of php w/mysql.

I have a table called 'testTable' with two columns, 'id' and 'testField'.
id is auto_incremented . I've got 4 rows basically like this:

id testField
1 some text
2 more text
3 some more text
4 and more text

I would just like to get the latest entry from the db which would be "and more text".

so here's my code so far:

Code: Select all

<?php
$conn = mysql_connect("localhost", "myusername","mypassword");
mysql_select_db("mydatabase_name", $conn);

$sql = "SELECT testField FROM testTable";
$result = mysql_query($sql, $conn) or die(mysql_error());
echo "$result";
?>
And the result i get from that query is this "Resource id #3".

How would i get only the latest entry from the db to display?
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Re: Selecting most recent entry from db

Post by foobar »

Try this:

Code: Select all

<?php
$conn = mysql_connect("localhost", "myusername","mypassword");
mysql_select_db("mydatabase_name", $conn);

/* Order by (id) descending and subsequently retrieve only the first element */
$sql = "SELECT testField FROM testTable ORDER BY id DESC LIMIT 1";
$result = mysql_query($sql, $conn) or die(mysql_error());

/* Fetch data from result set into an associative array */
$row = mysql_fetch_assoc($result);

/* Access any SELECT'ed fields via $row['field_name'] */
echo $row['testField'];
?>
dru_nasty wrote: And the result i get from that query is this "Resource id #3".
That's because you're trying to output something that you can't output...
A PHP Resource is a special type of its own.
Last edited by foobar on Sat Dec 03, 2005 3:03 pm, edited 1 time in total.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

what about

Code: Select all

WHERE id=max(id)
??
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

djot wrote:what about

Code: Select all

WHERE id=max(id)
??
You could do that to, I suppose. :)
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

Post by dru_nasty »

I'm getting this error now "Invalid use of group function"

Here's the sql statement:

Code: Select all

$sql = "SELECT testField FROM testTable WHERE id=max(id)";
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Use my code then.
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

Post by dru_nasty »

That did the trick, thanks so much. :D
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

shouldnt it be something like

Code: Select all

$sql = "SELECT max(id) as id_max FROM testTable";
?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

mickd wrote:shouldnt it be something like

Code: Select all

$sql = "SELECT max(id) as id_max FROM testTable";
?
yup.
Post Reply