Saturday 28 October 2017

mysql - PHP unkown error "mysqli_fetch_array()"





Good day good people.I have unkown error while trying to display
data from mysql to web using PHP.Error is:





mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
C:\xampp\htdocs\form_2\guestbook.php on line 17.
Line 17 is like
this:




while($row=mysqli_fetch_array($result))



I
really don't know where the problem is, so i hope u will help me.Thank u all
:D



Here is code i'am using
Here is
code that i am using.I just want to display data from database to
web.



$con=mysqli_connect("localhost","root","","php_tests");
if
(mysqli_connect_errno())

{
echo"Error connecting to
database". mysqli_connect_error();
}

$result =
mysqli_query($con,"SELECT * FROM
test_mysql");
while($row=mysqli_fetch_array($result))
{
$ime=$row['name'];
$prezime=$row['lastname'];
$id_number=$row['id'];

echo' border="1" width="33%">


Ime:

'.$ime.'



Prezime

'.$prezime.'




';
}
mysqli_close($con);

?>


Answer




It means your query execution failed. You
need to check if condition the result was false before trying to use
it.




PHP



if
(!mysqli_fetch_array($result)) {
printf("Error: %s\n",
mysqli_fetch_array($con));
exit();
} else{

while($row=mysqli_fetch_array($result)){
.....
// Your
code
}


}


mysqli_fetch_array()
returns FALSE if there was an error in the query. So you should
test for it before use while loop...



/>

Updated





$con=mysqli_connect("localhost","root","","php_tests");
if
(mysqli_connect_errno()){
echo"Error connecting to database".
mysqli_connect_error();
}

$result =
mysqli_query($con,"SELECT * FROM test_mysql");

while($row=mysqli_fetch_array($result))
{

$ime=$row['name'];
$prezime=$row['lastname'];


$id_number=$row['id'];
echo '














Ime: '.$ime.'
Prezime '.$prezime.'
';
}

mysqli_close($con);

?>


Result




src="https://i.stack.imgur.com/AaeZy.png" alt="enter image description
here">



PHPMyAdmin



src="https://i.stack.imgur.com/4S241.png" alt="enter image description
here">



without checking it work perfect but
in your case not working so you can try to check if condition. Hope this help
you!


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...