mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in

$sql="SELECT * FROM users WHERE id='$id'";
$result = mysqli_query($con, $sql);
 while($row = mysqli_fetch_array($result)) {
            
        $firstname  =   $row["firstname"];
        $lastname   =   $row["lastname"];
        $email      =   $row["email"];
        $password   =   $row["password"];
       
     //   echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }

Open in new window

warnig:mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in (give me solution)
kavya k naikAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
I don't think you've constructed your SQL statement quite correctly, try this.
$sql="SELECT * FROM users WHERE id='".$id."'";

Open in new window

mohan singhWeb developerCommented:
Julian HansenCommented:
$sql="SELECT * FROM users WHERE id='$id'";
$result = mysqli_query($con, $sql);
// CHECK THE QUERY WAS SUCCESSFUL
if ($result) {
  while($row = mysqli_fetch_array($result)) {
        $firstname  =   $row["firstname"];
        $lastname   =   $row["lastname"];
        $email      =   $row["email"];
        $password   =   $row["password"];
       
     //   echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
}
else {
  echo "Query failed with error: " . mysqli_error($con) . "<br>";
}

Open in new window

It is recommended that you employ robust coding mechanisms with code that could potentially fail - don't assume your result will hold a valid result.

@Norie it is perfectly legitimate to embed a variable in double quoted string as the author has it. My only recommendation would be to add {} around the variable. This is optional and typically used when the variable name is adjacent to output text that could make the name of the variable ambiguous - I use it all the time.
$sql="SELECT * FROM users WHERE id='{$id}'";

Open in new window

Vijaya KumarCommented:
when your query return empty records it shows like this so try this

$result = mysqli_query($con, $SQL)or die(mysqli_error($connection));
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.