Is there an easier way to use the result of an mysql_query()


#1

[code]$sqlGetAge = “select Age from users where UserId = ‘123’ limit 1”;

$result = mysql_query($sqlGetAge);

while ($row = mysql_fetch_array($result)){	
	$age = $row{'Age'};

}[/code]

The code will always return one value…the age of user with the UserId of 123. I’m wondering if there is an easier way to get to that number without having to do the while…loop. mysql_fetch_row doesn’t seem like it would work any better since I’ve only ever seen that in a loop too. Any ideas.

I wish it was as simple as:

[code]$sqlGetAge = “select Age from users where UserId = ‘123’ limit 1”;

$result = mysql_query($sqlGetAge);
$age = $result{'Age'};

}[/code]

Thanks
Jeff


#2

Hi Jeff,

As you are only fetching 1 row, how about mysql_fetch_object() ?

$query = mysql_query("select Age from users where UserId = '123' limit 1") or die(mysql_error()); $row = mysql_fetch_object($query);

echo $row->Age;

I changed your query syntax too.

Hope that helps. :)


#3

if you are only returning 1 cell of data try using mysql_result (http://www.php.net/manual/en/function.mysql-result.php). it is faster then any of the the mysql_fetchs.