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

Asked

Viewed 66 times

-2

The second selection of 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>";
        }
        ?>

<?

}

?>
  • People don’t forgive, 1 minute and 2 negative. They should use this effort to help, but anyway. I didn’t understand why you were using CALL Spmenusubconsulta(5) in a query. Can you explain?

  • A possible problem is the use of obsolete functions mysql_* that do not work properly with SP.

  • Um? I didn’t understand the first comments rs yes. Next... I need 2 selections in this case, take the data from one SP and use in another SP with the while

  • And which one would you use instead? @rray

  • Which error appears? is something like out Sync?

  • 1

    Exactly @rray "Commands out of Sync; you can’t run this command now"

  • It is possible to solve in some ways, if it is not possible to exchange the functions for the MySQLi or PDO can store the result of the first SP in an array after doing a foreach with it and calling the second inside, maybe there is another way but these are the ones I remember now xD

  • Already great brother @rray I’m going to test these two, solving back to publish the resolution!! Tks, thanks Brothers

  • Diego, I think the same thing expensive, If they used this to help would be much more productive, it seems that they have already accustomed to negativize everything, I asked a question and in a few minutes they had 2 negatives and soon after rose to 2 positive and 2 response each with 1 postivo... Now think... Those who negatively they negatively why?? if the question was inside the conforms and gained positivity?? See? There’s something very wrong about it People prefer to just get in the way instead of helping.

  • Got it now for Diego! really rs

Show 5 more comments

2 answers

1


Brother, I solved the problem using an ARRAY, check it out! gave it right!

                <?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


                ?>

0

Commands out of Sync; you can’t run this command now

This error usually happens when using stored procedures with the functions mysql_*, the message says something like "I can’t process that query because I haven’t finished dealing with the previous one."

Ideal is to migrate the code from the functions mysql_* for Mysqli’s, if this is not possible, process the first query and store it in an array and then itere it calls 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>";
      }
    }
  • Strange, I tried and returned the same error "Commands out of Sync; you can’t run this command now"

  • In Place in (5) I would like to take the value of $Codattribute tovalor -- `CALL Spmenusubconsulta($Codattribute tovalor)

  • @Josiasviskoo made a mistake? In theory it’s the same thing

  • Apparently it was wrong too... maybe there is some different alternative to do what I’m thinking of doing... maybe this practice is wrong... because in the web and the manual there is nothing of the kind... what I’m trying to do is a menu and submit from a query variant. The First SP returns categories and the second SP returns the Ids of categories that bring other subcategories, I thought of doing in the same SP, but I will use this SP on other pages...

  • @Josiasviskoo even running the first query in a while da o out of Sync?

  • Yes, it gave the same error... strange... even individual also happens this, but the difference between them is that one is parameterized and one is not... maybe mysql_query does not support SP parameter, it will be?

  • @Josiasviskoo the problem is that you try to run the second query without having just treated the first, I see here other things ...

  • Um understood... from what I understood is this, but I could not address this issue on this possibility... @rray

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.