Sometimes, we want to get a single result from a database using mysqli and PHP.
In this article we’ll look at how to get a single result from a database using mysqli and PHP.
How to get a single result from a database using mysqli and PHP?
To get a single result from a database using mysqli and PHP, we can use the mysqli_fetch_row
function.
For instance, we write
$query = "SELECT ssfullname, ssemail FROM userss WHERE user_id = " . $user_id;
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_row($result);
$ssfullname = $row["ssfullname"];
$ssemail = $row["ssemail"];
to call mysqli_query
with the $query
string to run the query.
Then we call mysqli_fetch_row
with $result
to get the first result as an associative array.
Then we can access the fields in the row with
$ssfullname = $row["ssfullname"];
$ssemail = $row["ssemail"];
Conclusion
To get a single result from a database using mysqli and PHP, we can use the mysqli_fetch_row
function.