Skip to content
Advertisement

Return One Row from MySQL

Amateur question, but something I’ve been wondering about. What is the PHP for selecting one row from a MySQL query? AKA you’re picking something by unique ID (you know there’s only one row that matches your request) and want to get only that value. Do you still have to use a while loop and mysql_fetch_row?

$query = "SELECT name,age FROM people WHERE uid = '2'";
$result = mysql_query($query);

// what php is used to return the name and age?  preferably without a loop?

Advertisement

Answer

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"]; 
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement