This is my first time trying to use mySQLi. In the case of a loop, I've done this. The results of the loop show up, but when I try to show a single record it gets stuck. Here is the working loop code.
Name:
Email:
How do I display a single record, any record, name or email, from the first row or anywhere else, just a single record, how do I do this? In case of single record, consider removing the above loop part and let us display any single record without using loop.
Use
mysqli_fetch_row()
. Try the following code:When only one result is needed, there is no need to use a loop. Get the row directly.
If you need to obtain the entire row of data as an associative array:
If only one value is required, starting with PHP 8.2:
Or for older versions:
Here are complete examples of different use cases
Variables used in queries
When variables need to be used in a query,preprocessing statementsmust be used. For example, let's say we have a variable
$id
:PHP >= 8.2
Older versions of PHP:
A detailed explanation of the above process can be found in myarticle. The reason why this procedure must be followed is explained in this famousQuestion.
There are no variables in the query
In your case, no variables are used in the query, you can use the
query()
method: