• Register
0 votes
1.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 50 views
50 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 12 views
12 views
I need to solve this error any one from kodlogs can help me?
asked Oct 4, 2020 Daniel Anderson 4k points
0 votes
1 answer 18 views
18 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, 2020 dorothy 300 points
0 votes
1 answer 43 views
43 views
Problem: I am very new to php and mysqli. I am facing below strange looking warning: Mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in. Please find below my code: <?php session_start(); $db=mysqli_connect("localhost","aaron","","demo"); $id=$_GET[" ... view.php"); } ?> I am trying to fix the above mysqli error from the past couple of days but still unable to fix it.
asked Aug 29, 2020 Raphael Pacheco 4.9k points
1 vote
1 answer 50 views
50 views
Problem: I am very new to the Mysqli_*. I have currently started understanding it. I am facing following errors: Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in D:\Hosting\9864230\html\includes\connection.php on line 11 Warning: mysqli_error( ... ) {     die("The Database selection failed: " . mysqli_error()); } ?> Kindly help me in neutralizing above errors.
asked May 28, 2020 Martin K 6.6k points
0 votes
1 answer 12 views
12 views
We are attempting to select data from a MySQL table, however we are getting this error.
asked Sep 28, 2020 Daniel Anderson 4k points
0 votes
1 answer 25 views
25 views
This issue stuck in my path "Warning: mysqli_query() expects at least 2 parameters, 1 given". I am finding the solution in several blogs and lot's of opinion coming. Can someone provide me the specific solution and script for solving this issue?
asked Sep 14, 2020 Daniel Anderson 4k points