Skip to content
Advertisement

Php -MySQL query failing – error Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean

I have following php code :

<?php
    $connect = mysqli_connect("localhost","root", "","archit") or die("Couldn't connect to database");
    $str = "SET @id := (SELECT ID FROM users where Name ='$u_name'); SELECT UID FROM useratt where ID = '@id';";
    echo $str;
    $query12 = mysqli_multi_query($connect,$str);
    echo $query12;
    while($row = $query12->fetch_assoc())
    {
        $str1 = "SET @UID := '".$row."'; 
         SELECT AttributeValue FROM att_value where UID=@UID; 
         SET @AID := (SELECT AID FROM att_value WHERE UID=@UID);
         SELECT AttributeName FROM att_name WHERE AID=@AID;";
         echo $str1;
        $query1= mysqli_multi_query($connect,$str1);
        echo $row[0];
    }
?>

I get the generic error but when I run the same code on MySQL then it works without error. Can someone please help me what am I missing .

I even tried exception handling but it didn’t help.

NOTE : code fails in the while loop condition.

Advertisement

Answer

The mysqli_multi_query() executes one or multiple queries which are concatenated by a semicolon.To retrieve the resultset from the first query you can use mysqli_use_result() or mysqli_store_result(). All subsequent query results can be processed using mysqli_more_results() and mysqli_next_result().mysqli_multi_query() only returns FALSE if the first statement failed. To retrieve subsequent errors from other statements you have to call mysqli_next_result() first.

if (mysqli_multi_query($connect,$str))
 {

     do
    {
        if ($result=mysqli_store_result($connect)) {

          while ($row=mysqli_fetch_row($result))
            {
                    $str1 = "SET @UID := '".$row."'; 
                    SELECT AttributeValue FROM att_value where UID=@UID; 
                    SET @AID := (SELECT AID FROM att_value WHERE UID=@UID);
                    SELECT AttributeName FROM att_name WHERE AID=@AID;";
                    echo $str1;
                    $query1= mysqli_multi_query($connect,$str1);
                    echo $row[0];
            }

          mysqli_free_result($result);
          }
    }
    while (mysqli_next_result($connect));
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement