Smit Saraiya Smit Saraiya - 10 months ago 50
MySQL Question

how to get value from select query?

How to get value from select query without using while loop while we know that output is defiantly only one record

$sql = "SELECT id FROM MyGuests";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
echo $row["id"];
}


Here if i know that there is only one record comes as a output then how to avoid while loop and directly get our id

Answer Source

just delete the while loop!

$sql = "SELECT id FROM MyGuests";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row["id"];

are you using mysql_* functions by any chance? please switch to PDO as soon as possible.