title: Call mysql PROCEDURE inside while loop works only first time
body: <p>I have some data from the DB I post on a table. For every line of the table (every loop of the while loop), I need to CALL a stored procedure to calculate the data for the last <code>&lt;td&gt;</code> of my table.</p>
  
  <p>Here some code:</p>
  
  <pre><code>$s = $lk-&gt;query("SELECT *
                    FROM A_USERS
                    JOIN A_DATA
                    WHERE A_DATA.id_user = A_USERS.id
                      AND A_USERS.usr_att = 1
                      AND A_DATA.act_data = 1
                      AND A_DATA.a_att = 1
                      AND A_DATA.qty IS NOT NULL
                    ORDER BY ID ASC");
  
  
  while ($dato = $s-&gt;fetch_object()) {
      print '&lt;tr&gt;';
  
      print '&lt;td&gt;';   
      print $dato-&gt;id;
      print '&lt;/td&gt;';
  
      //others cells
  
  
      $GetP = $lk-&gt;query("CALL GetPr($dato-&gt;qty, '$dato-&gt;prod')");
  
      if(mysqli_num_rows($GetP) === 1){
          while($p_db = mysqli_fetch_array($GetP)){
              $p = $p_db['p'];
          }
  
          $format = number_format(round($p, 1), 2, ',', '\'');
  
          //some other stuff here...
  
      }else{
          print '&lt;td&gt;';
          print 'ERROR';
          print '&lt;/td&gt;';
      }
      print '&lt;/tr&gt;';
  }
  </code></pre>
  
  <p>The problem is that ONLY the first loop of the <code>while</code> calls the procedure which returns <code>1 row</code>. Else I get always <code>ERROR</code> on this <code>&lt;td&gt;</code> which means the stored procedure didn't return <code>1 row</code>.</p>
  
  <p>Why?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Stored procedure:</p>
  
  <pre><code>CREATE DEFINER=`...` PROCEDURE `GetPr`(IN `cli_qty` INT, IN `cli_prod` VARCHAR(10) CHARSET ascii)
  BEGIN
      SET @cli_qty = cli_qty;
  
      SET @q = (SELECT qty FROM MRG_H ORDER BY ABS(@cli_qty - qty) ASC LIMIT 1);
  
      SET @cli_prod = cli_prod;
  
      IF @cli_prod = 'OECO' THEN SET @marg = (SELECT (m+v) FROM MRG_H JOIN DT WHERE m &lt;&gt; '' AND q = @q AND par = 'MarOECO');
  
      ELSE SET @marg = (SELECT m FROM MRG_H WHERE m &lt;&gt; '' AND q = @q);
      END IF;
  
      SELECT (Med+@marg)*((Val+100)/100) AS P, D_aaaammgg AS Date
          FROM PL
            JOIN TVA
          WHERE I_D = 0
        AND Med &lt;&gt; ''
        AND Date_f = ''
          ORDER BY Iden DESC
          LIMIT 1;
  END
  </code></pre>
  
  <p>MYSQLI ERROR:</p>
  
  <pre><code>Error: 2014-Commands out of sync; you can't run this command now
  </code></pre>