0

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'>
1
  • 4
    You should pass the_result_ of mysqli_query() to mysqli_fetch_array(), not the query itself ... Commented Jul 20, 2015 at 8:33

3 Answers 3

4

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).

Sign up to request clarification or add additional context in comments.

Comments

1

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)){
    }
}

Comments

1

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;
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.