a simple query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
siyaco
Forum Commoner
Posts: 26
Joined: Mon Feb 05, 2007 1:36 am
Location: Kurdistan Mountains

a simple query

Post by siyaco »

i have a table which includes userid(auto increment), username, message

i want to write a query which selects the name and message from the row has the biggest id
and then i want to give the values of that name and message into two variables with php
how can i do?
thanks all in advance
siyaco
Forum Commoner
Posts: 26
Joined: Mon Feb 05, 2007 1:36 am
Location: Kurdistan Mountains

Post by siyaco »

i have just solved the problem, may be it is easy to you but again i am writing what i did

1- $query='SELECT name, message FROM table_name where id=(SELECT max(id) from table_name)'

2-$line=mysql_fetch_array($query, MYSQL_NUM);
echo $line[0]; // gives the name
echo $line[1]; // gives the message


thanks all in advance
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

SELECT foo FROM bar ORDER BY id DESC, LIMIT 1
siyaco
Forum Commoner
Posts: 26
Joined: Mon Feb 05, 2007 1:36 am
Location: Kurdistan Mountains

Post by siyaco »

feyd wrote:

Code: Select all

SELECT foo FROM bar ORDER BY id DESC, LIMIT 1
i knew that it is so easy to you , how didn't i think... :D
Post Reply