0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ria Sen
  • 94
  • 13

3 Answers3

2

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));
}
Pramod Patil
  • 2,704
  • 2
  • 14
  • 20
  • It will be great if you explain your answer so that it is easy for others to understand the reason. – Ria Sen Mar 31 '17 at 05:29
1

A. The first problem is your SELECT, you write:

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

You must remove the quote at @id variable to this:

SELECT UID FROM useratt where ID = @id;

B. After mysqli_multi_query execute, you must add two others call to get result:

  1. Your query will return two result, first result is for set variable. And the second is that your data query. Thus, we next to move next to second result to get data:

    mysqli_next_result($connect);

  2. After move next to second result, we must get result using mysqli_store_result:

    $query12 = mysqli_store_result($connect);

Hope this can help you!

Sang Lu
  • 308
  • 3
  • 10
-1
<?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];
    }
?>
Sudhakar
  • 324
  • 3
  • 9