Return One Row from MySQL
Add limit 0,1
and for PHP 7 use mysqli
$query = "SELECT name,age FROM people WHERE uid = '2' LIMIT 0,1";
$result = mysqli_query($query);
$res = mysqli_fetch_assoc($result);
echo $res["age"];
If uid
is your primary key, or it has a unique
constraint, you can simply call mysql_fetch_row($res)
once.
If you want to verify that you actually got a result, you can check if mysql_num_rows($res) == 1
.