george
george

Reputation: 21

error mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given

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

this is my codes can anyone tell me what is wrong?

$result ="SELECT * FROM report" ;
if(mysqli_query($cons, $result)) {
echo("
<div class='sc'>
<table  id='to1' width='90%' border='0' cellspaceing='1' cellpadding='8' align='center'>
 <tr bgcolor='#9B7272'>
<td > ID </td>
<td > first_name </td>
<td > last_name </td>
<td > phone </td>
<td > address </td>
<td > email </td>
<td > birthdate </td>
<td > gender </td>
<td > city </td>
<td > dr_name </td>

</tr>

");
while($row = mysqli_fetch_array($result))
{
$ID         =$row['ID']; 
$first_name =$row['first_name'];
$last_name  =$row['last_name'];
$phone      =$row['phone'];
$address    =$row['address'];
$email      =$row['email'];
$birthdate  =$row['birthdate'];
$gender     =$row['gender'];
$city       =$row['city'];
$dr_name    =$row['dr_name'];

echo "  <tr bgcolor='#C7B8B8'>

Upvotes: 0

Views: 6928

Answers (3)

Sakura
Sakura

Reputation: 93

you can write like this:-

$query = mysqli_query($cons,"SELECT * FROM items WHERE id = '$id'");
if (mysqli_num_rows($query) > 0)
{
while($row = mysqli_fetch_assoc($query))
{
  $result=$row;
}
}

Upvotes: 1

D4V1D
D4V1D

Reputation: 5849

Problem

You are missing how to pass the argument to mysqli_fetch_array().

Solution

Therefore, this line:

if(mysqli_query($cons, $result)) {

should be

if($res = mysqli_query($cons, $result)) { // assign the return value of mysqli_query to $res

(FWIW, I'd go with $res = mysqli_query($cons, $result); then do if($res) {.)

And then do

while($row = mysqli_fetch_array($res)) // pass $res to mysqli_fetch_array instead of the query itself

Why?

You were giving to mysqli_fetch_array() - as an argument - the string that contains your query. That's not how it works. You should pass the return value of mysqli_query() instead. Therefore, you could also write: while($row = mysqli_fetch_array(mysqli_query($cons, $result))) {} (but it's not adviced, it is just to show you how it works).

Upvotes: 4

dan richardson
dan richardson

Reputation: 3939

As mentioned in the comment, you need to set a $result from your query and use that in your loop instead.

$qry ="SELECT * FROM report";
$result = mysqli_query($cons, $qry);
if ($result){
    while($row = mysqli_fetch_array($result)){
    }
}

Upvotes: 1

Related Questions