title: My SQL Dynamic query execute and get ouput into a variable in stored procedure
body: <p>I generate a dynamic query in My sql Stored procedure. I wanna get the result of this query into a out parameter. How to do this ?</p>
  
  <pre><code>CREATE PROCEDURE 'searchInvoice'
  (
    OUT numOfRecords INT
  )
  BEGIN
    DECLARE query1 TEXT; 
    DECLARE query2 TEXT; 
  
   SET query1 = 'SELECT COUNT(*) bla bla bla.....'; 
   // Query1 to select the count of matching tuples..
  
   SET query2 = 'SELECT * from bla bla bla....';
   // Query2 to select original records...
  
   // later part of this both queries generate dynamically according to some IN parameters..
  
   // now I wanna assign the output of the query1 into numOfRecords 
   // and I wanna execute the query2 as well.. like this
  
      SET @Sql = query2;        
      PREPARE STMT FROM @Sql; 
      EXECUTE STMT; 
      DEALLOCATE PREPARE STMT;
  
   // output of the query2 can be read in PHP
  
  END
  </code></pre>
  
  <p>How to get the output of the query1 into OUT parameter(<code>numOfRecords</code> ) ??</p>