obreezy obreezy - 13 days ago 5
MySQL Question

What's wrong with my query? Cannot fetch records

My query isn't working. It does not print the record, but in phpmyadmin it does bring up a record.

$sql1 = "SELECT fullname FROM addresses WHERE userid = 1 LIMIT 1";
$result1 = mysqli_query($conn, $sql1);

while ($row = mysqli_fetch_row($result1)) {

$fullname = $row['fullname'];

$i++; }
echo $fullname;


UPDATE:
I don't understand the down votes. If nothing is wrong with the code, please say so. Anyway, it was that my table did not have a unique key.

Answer

You can use mysqli_fetch_assoc for get single records instead of mysqli_fetch_row function. You didn't need while loop for set single records from database. Below i have updated your code for single record.

 $query = "SELECT fullname FROM addresses WHERE userid = 1 LIMIT 1";
 $result = mysqli_query($conn, $query);
 $row = mysqli_fetch_assoc($result);
 echo "Full Name:". $row['fullname'];
Comments