how to fetch the value from a query to a variable

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
imsreejith2478
Forum Newbie
Posts: 1
Joined: Thu Apr 24, 2008 5:34 am

how to fetch the value from a query to a variable

Post by imsreejith2478 »

hi all,
i am new to php and i would like to know how to get the value from a query set to a variable.

eg is.

my query is
select max(columnname) from table_name;

i want to get this value retrieved from the query to a variable.

regards,

sreejith
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: how to fetch the value from a query to a variable

Post by aceconcepts »

Here's the code - simplistic version:

Code: Select all

 
$query="select max(columnname) from table_name"; //SETUP QUERY
 
$result=mysql_query($query); //EXECUTE QUERY
 
$row=mysql_fetch_array($result); //FETCH QUERY RESULTS AS AN ARRAY
 
if you want to output multiple tables rows then you will need to loop the result array:

Code: Select all

 
while($row=mysql_fetch_array($result))
{
   echo $row['field_name'];
}
 
Post Reply