• Register
0 votes
2.1k views

Problem :

I am very new to PHP and MySQL and I just can't figure this one out. I have searched on all around the forums but haven't found the answer I can make sense of. I originally was using mysql_fetch_assoc() but I could only search numbers and I received errors as below when searching for letters as well. I hope I am on a right track here. Thank you in advance for all of your help!

My Error :

“Mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in”

7 5 2
3,870 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

This happens when your result is not the result (but the "false" instead). You should change this line

$sql = 'SELECT * FROM $usertable WHERE PartNumber = $partid';

To below line:

$sql = "SELECT * FROM $usertable WHERE PartNumber = $partid";

As the " can interprete a $variables while ' cannot.

Works fine with the integers (numbers), for strings you need to put a $variable in single quotes, like

$sql = "SELECT * FROM $usertable WHERE PartNumber = '$partid' ";

If you want  or have to work with a single quotes, then php CAN NOT interprete a variables, you will have to do it like below:

 $sql = 'SELECT * FROM '.$usertable.' WHERE string_column = "'.$string.'" AND integer_column = '.$number.';

9 7 4
38,600 points
0 votes

Solution:

1. This error is thrown because the query was not executed correctly, before entering While(..) {...} , check the result returned:

if($sql === FALSE) {
    // Consulta falhou, parar aqui 
    die(mysqli_error());
}

Follow the below code

$sql = mysqli_query($conexao, "SELECT diasvip FROM login WHERE userid = '".$_POST['userid']."'");
if($sql === FALSE) { 
   die(mysqli_error());
}

while($exibe = mysqli_fetch_assoc($sql)){
  echo "<div class='alert alert-success' role='alert'>Dias Vip do Usuário: ".$exibe['diasvip']."</div>";  
}
mysqli_close($conexao);

 

  1. This error happens when your query returns an error, to debug it, create a variable with the sql print it, and test it directly on the database, also enable errors when executing the query with mysqli_error

$str_consulta = "SELECT diasvip FROM login WHERE userid = ".$_POST['userid']);
echo $str_consulta;
$sql = mysqli_query($conexao, $str_consulta)  or die(mysqli_error($db));

 

  1. The return value of mysql_query is a mixed type. It is a boolen value or a resource upon success for specific query types.

Check the return value before fetching results:

if ($query = mysql_query($sql)) {
    while ($users=mysql_fetch_assoc($query)){
        // ...
    }
} else {
    // Report error
}
  1. This happens when your result is not a result (but a "false" instead). You should change this line

$sql = 'SELECT * FROM $usertable WHERE PartNumber = $partid';

To This

$sql = "SELECT * FROM $usertable WHERE PartNumber = $partid";

because the " can interprete $variables while ' cannot.

Works fine with integers (numbers), for strings you need to put the $variable in single quotes, like

$sql = "SELECT * FROM $usertable WHERE PartNumber = '$partid' ";

If you want / have to work with single quotes, then php CAN NOT interprete the variables, you will have to do it like this:

$sql = 'SELECT * FROM '.$usertable.' WHERE string_column = "'.$string.'" AND integer_column = '.$number.';

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 52 views
52 views
Problem: mysqli_fetch_assoc() expects parameter / Call to a member function bind_param() errors. How to get the actual mysql error and fix it?
asked Mar 15 ummesalma 29.2k points
0 votes
1 answer 14 views
14 views
Problem: My Error : &ldquo;Mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in&rdquo; , any help!!
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 3 views
3 views
Problem: I have some trouble checking if a Facebook User_id already exists in my database, my localhost it gives me the following error: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
asked Apr 24 ummeshani 10.8k points
0 votes
1 answer 15 views
15 views
Problem: I have read a lot of these and they are for the most part and error in the database connection or the query. I am getting this error and I have a good connection and a good query. I know this because I print_r or echo the results of the $row and I get matching results. Please help?
asked Apr 12 Sana8989 8.9k points
0 votes
1 answer 182 views
182 views
I have faced this issue in several times and finding diddiculty to solve this issue. Can someone help me to solve this issue?
asked Sep 14, 2020 Daniel Anderson 4k points
0 votes
1 answer 8 views
8 views
Problem: What is the solution mysqli_ num rows expects parameter 1 to be the mysqli result boolean value specified in?
asked Apr 14 jamuna1 30.2k points
1 vote
1 answer 11 views
11 views
Problem: I am getting these errors but need solution: Warning: mysqli_select_db() expects parameter 1 to be mysqli.
asked Apr 28 PkGuy 23.5k points
0 votes
1 answer 27 views
27 views
I need to solve this error any one from kodlogs can help me?
asked Oct 4, 2020 Daniel Anderson 4k points