This is my first time trying mySQLi. I've done it in a loop. The loop results are showing, but when I try to show a single record, I get stuck. This is the loop code in action.
Name:
Email:
How do I display a single record, any record, name or email, from the first row or whatever, just a single record, how do I do that? In case of single record, consider removing all the above looping parts and let us display any single record without looping.
Use
mysqli_fetch_row()
. Try this,Loops should not be used when only a single result is required. Just get the row now.
If you need to extract the entire row into an associative array:
If you only need one value, starting with PHP 8.2:
Or for older versions:
Here are complete examples of different use cases
Variables used in queries
When you want to use variables in a query, you must useprepared statements. For example, let's say we have a variable
$id
:PHP >= 8.2
Old PHP version:
A detailed explanation of the above process can be found in myarticle. As to why it must be followed, see this famousquestion
There are no variables in the query
In your case, if no variables are used in the query, you can use the
query()
method: