The second QUERY of Store Procedure does not run ... I did not find the bug

-2

The second selection of the Store Procedure does not run ... I did not find the bug ...

<? require("viskoo_conexao/conectar.php");?>


<?php

$consulta="CALL SPMenuConsulta";

$resultado = mysql_query($consulta)

or die("Falha na SP (1)");

?>




<?php

while ($linha = mysql_fetch_assoc($resultado))

{

$ValorAtributo = $linha[ValorAtributo];

$CodAtributoValor = $linha[CodAtributoValor];


echo $ValorAtributo." ($CodAtributoValor)<br>";

?>
        <? $resultado2 = mysql_query( 'CALL SpMenuSubConsulta(5)' ) or die(mysql_error()); ?>

        <?php
        while ($linha2 = mysql_fetch_assoc($resultado2))
        {
        $ValorAtributo = $linha2["ValorAtributo"];
        echo "--Sub".$ValorAtributo."<br>";
        }
        ?>

<?

}

?>
    
asked by anonymous 02.03.2016 / 21:40

2 answers

1

Brother, I solved the problem using an ARRAY, look! It worked!

                <?php
                $ArrayMenuAtr = array();

                    $mysqli = new mysqli('localhost', 'loja', 'Pass', 'loja' );
                    $result = $mysqli->query("CALL SPMenuConsulta()");

                    if( !$result ) {

                        die('Query failed returning error: '. $mysqli->connect_errno );

                    } else {

                        while( $row = $result->fetch_array(MYSQLI_ASSOC)) {
                            $ValorAtributo        =  $row['ValorAtributo'];
                            $CodAtributoValor        =  $row['CodAtributoValor'];

                            $ArrayMenuAtr[$CodAtributoValor] = $ValorAtributo;

                        }
                    }
                ?>



                <?

                foreach ($ArrayMenuAtr as $id => $categoria):

                echo "Menu: $categoria ($id) <br>";

                $mysqli = new mysqli('localhost', 'loja', 'Pass', 'loja' );
                    $result = $mysqli->query("CALL SpMenuSubConsulta($id)");

                    if( !$result ) {

                        die('Query failed returning error: '. $mysqli->connect_errno );

                    } else {

                        while( $row = $result->fetch_array(MYSQLI_ASSOC)) {
                            $ValorAtributo        =  $row['ValorAtributo'];

                            echo "Sub- ".$ValorAtributo."<br>";
                        }
                    }

                endforeach


                ?>
    
04.03.2016 / 19:44
0
  

Commands out of sync; you can not run this command now

This error usually happens when using stored procedures with the mysql_* functions, the message says something like "I can not process this query because I have not finished processing the previous one."

The ideal is to migrate the code from mysql_* to MySQLi functions, if this is not possible, process the first query and store it in an array and then iterate it to the second stored procedure.

<?php
   $consulta="CALL SPMenuConsulta";
   $resultado = mysql_query($consulta) or die("Falha na SP (1)". mysql_error());

   $arr = array();
   while ($linha = mysql_fetch_assoc($resultado)){ 
      $arr[] = $linha;
   }

   foreach($arr as $item){  
      $ValorAtributo = $item['ValorAtributo'];
      $CodAtributoValor = $item['CodAtributoValor'];
      $resultado2 = mysql_query( 'CALL SpMenuSubConsulta(5)') or die(mysql_error());
      while ($linha2 = mysql_fetch_assoc($resultado2)){
         $ValorAtributo = $linha2["ValorAtributo"];
         echo "--Sub".$ValorAtributo."<br>";
      }
    }
    
02.03.2016 / 22:40