• Register
0 votes
455 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

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
0 answers 11 views
11 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 Daniel Anderson 4k points
0 votes
1 answer 5 views
5 views
I need to solve this error any one from kodlogs can help me?
asked Oct 4 Daniel Anderson 4k points
0 votes
1 answer 7 views
7 views
Hello Kodlogs, I was trying to access my database using PHP code. The code snippets I included below shows my code. This code should be able to display the my station name. But I got a an error: &lsquo;mysqli_num_rows() expects parameter 1 to be mysqli_result&rsquo;. $query ... my head on this because I can&rsquo;t see any problem with my code. So I hoped that you could help me. Thank you!
asked Oct 11 dorothy 180 points