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

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

我有以下 php 代码:

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

我得到了一般错误,但是当我 运行 在 MySQL 上使用相同的代码时,它可以正常工作。有人可以帮我看看我错过了什么吗?

我什至尝试过异常处理,但没有用。

注意:代码在 while 循环条件下失败。

mysqli_multi_query() 执行一个或多个查询,这些查询由 semicolon.To 连接,从第一个查询中检索结果集,您可以使用 mysqli_use_result() 或 mysqli_store_result()。所有后续查询结果都可以使用 mysqli_more_results() 和 mysqli_next_result() 进行处理。mysqli_multi_query() 只有 returns FALSE 如果第一个语句失败。要从其他语句中检索后续错误,您必须先调用 mysqli_next_result()。

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));
}
<?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 = mysqli_fetch_assoc($query12))
    {
        $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];
    }
?>

A.第一个问题是你的SELECT,你写:

SELECT UID FROM useratt where ID = '@id';

您必须删除 @id 变量中的引号:

SELECT UID FROM useratt where ID = @id;

B. mysqli_multi_query 执行后,必须加上另外两个调用才能得到结果:

  1. 您的查询将 return 两个结果,第一个结果用于设置变量。第二个是您的数据查询。因此,我们接下来移动到第二个结果旁边以获取数据:

    mysqli_next_result($connect);

  2. 移动到第二个结果后,我们必须使用 mysqli_store_result:

    得到结果

    $query12 = mysqli_store_result($connect);

希望对您有所帮助!