Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in

Tags:

php

mysqli

I've got a little bit of a problem when I try to run this code. I receive this error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in

My problem is in the while statement on line 32:

$connection = mysqli_connect($host, $username, $password, $db_name);
$sql = 'SELECT * FROM provinsi';
while ($r = mysqli_fetch_array($connection, $sql)) { // line 32
    $id = $r['id'];
}
like image 385
age saputra Avatar asked Oct 24 '25 09:10

age saputra


1 Answers

mysqli_fetch_array()'s 1st parameter must be a result of a query. What you are doing is you are passing the connection (which doesn't make sense) and the query command itself.

Read the doc here: http://php.net/manual/en/mysqli-result.fetch-array.php

To fix this, execute the query first, then store the result to a variable then later fetch that variable.

$sql = "select * from privinsi";
$result = mysqli_query($connection,$sql);
while($r = mysqli_fetch_array($result))
{
    // your code here
}
like image 85
Lorence Hernandez Avatar answered Oct 27 '25 00:10

Lorence Hernandez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!