Page 1 of 1

how to fetch the value from a query to a variable

Posted: Thu Apr 24, 2008 5:40 am
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

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

Posted: Thu Apr 24, 2008 5:49 am
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'];
}