tags:

views:

59

answers:

2

I am trying to run the following.

<?php

$db = mysqli_connect("localhost","user","pw") or die("Database error");
mysqli_select_db($db, "database");

$agtid = $_POST['level'];

$sql = sprintf("call agent_hier(%d)", $agtid);

$result = mysqli_query($db, $sql) or exit(mysqli_error($db));

if ($result) {
    echo "<table border='1'>
        <tr><th>id</th>
        <th>name</th>
        <th>parent_id</th>
        <th>parent_name</th>
        <th>level</th>
        <th>email</th></tr>";

    while ($row = mysqli_fetch_assoc($result)) 
    {
        $aid = $row["id"];
        $sql2 = "SELECT * FROM members WHERE MEMNO = '$aid'";
        $result2 = mysqli_query($db,$sql2) or exit(mysqli_error($db));

            while ($newArray = mysqli_fetch_array($result2)) {
                $fname = $newArray['FNAME'];
                $lname = $newArray['LNAME'];
                $mi = $newArray['MI'];  
                $address = $newArray['ADDRESS'];    
                $city = $newArray['CITY'];  
                $state = $newArray['STATE'];    
                $zip = $newArray['ZIP'];
                            $kdate = $newArray['KDATE'];
                $date = abs(strtotime(date('m/d/Y')) - strtotime(date($kdate))) / (60 * 60 * 24);
            }

        echo sprintf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
            $row["id"],$row["name"],
            $row["parent_id"],$row["parent_name"],
            $row["level"],$row["email"]);
    }

    echo "</table>";
}

mysqli_free_result($result);
mysqli_close($db);

?>

If I remove lines from:

$aid = $row["agent_id"];

to....

$date = abs(strtotime(date('m/d/Y')) - strtotime(date($kdate))) / (60 * 60 * 24); }

everything will work fine. If not, I get the following error:

Commands out of sync; you can't run this command now

In researching, I think it could be due to multiple mysqli queries run at the same time, in which using mysqli_multi_query but for all the samples and general data in the guide does not seem to be applicable.

Any ideas?

A: 

It's because you call stored procedure/function that returns a result set. You must use mysqli_multi_query.

UPDATE It can be something like this (I'm used to object-oriented style, $mysqli is an instance of MySQLI :

 if ($result = $mysqli->multi_query( $sql))
 {
 do 

  {
    if ($res = $mysqli->store_result())
    {
       while ($row = $res->fetch_row())
       {
           $aid = $row["id"];
           // your code goes here
       }         
       $res ->close();
    }
  }
  while ($mysqli->next_result())
 } 
 else
 {
     die($mysqli->error);
 }
a1ex07
@a1ex07 - I talk about this in my post. Please read and let me know advice. The queries presented in examples don't come close to applying. I am using the results of one query to find the results of another.
JM4
Updated... Hope that helps
a1ex07
@a1ex07 - Did you try to read my post? This is now the second reply that directly conflicts with what I've said and is taken also directly from the link I already have posted in my question. I appreciate you taking the time out of your day to try and help but come on man
JM4
+1  A: 

The MySQL client does not allow you to execute a new query where there are still rows to be fetched from an in-progress query. See Commands out of sync in the MySQL doc on common errors.

You can use mysqli_store_result() to pre-fetch all the rows from the outer query. That will buffer them in the MySQL client, so from the server's point of view your app has fetched the full result set. Then you can execute more queries even in a loop of fetching rows from the now-buffered outer result set.

Or you mysqli_result::fetch_all() which returns the full result set as a PHP array, and then you can loop over that array.

Calling stored procedures is a special case, because a stored procedure has the potential for returning multiple result sets, each of which may have its own set of rows. That's why the answer from @a1ex07 mentions using mysqli_multi_query() and looping until mysqli_next_result() has no more result sets. This is necessary to satisfy the MySQL protocol, even if in your case your stored procedure has a single result set.


PS: By the way, I see you are doing the nested queries because you have data representing a hierarchy. You might want to consider storing the data differently, so you can query it more easily. I did a presentation about this titled Models for Hierarchical Data with SQL and PHP. I also cover this topic in a chapter of my book SQL Antipatterns: Avoiding the Pitfalls of Database Programming.

Bill Karwin
@Bill - Thanks for the great advice. I ended up solving my own question (so far...) in a manner which made more sense on another php page (http://php.net/manual/en/mysqli.store-result.php). Great presentation by the way - i need to give it some real time but seems valuabe. At first glance, however - may be a bum for me since i'm using MySQL - i'm sure similar theories apply
JM4