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

kavya k naik
kavya k naik used Ask the Experts™
on
$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)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

Commented:
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
$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

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

$result = mysqli_query($con, $SQL)or die(mysqli_error($connection));

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial