title: How to echo a MySQLi prepared statement?
body: <p>I'm playing around with MySQLi at the moment, trying to figure out how it all works. In my current projects I always like to echo out a query string while coding, just to make sure that everything is correct, and to quickly debug my code. But... how can I do this with a prepared MySQLi statement?</p>
  
  <p>Example:</p>
  
  <pre><code>$id = 1;
  $baz = 'something';
  
  if ($stmt = $mysqli->prepare("SELECT foo FROM bar WHERE id=? AND baz=?")) {
    $stmt->bind_param('is',$id,$baz);
    // how to preview this prepared query before acutally executing it?
    // $stmt->execute();
  }</code></pre>
  
  <p>I've been going through this list (<a href="http://www.php.net/mysqli" rel="noreferrer"><a href="http://www.php.net/mysqli" rel="noreferrer">http://www.php.net/mysqli</a></a>) but without any luck.</p>
  
  <p><br /><b>EDIT</b></p>
  
  <p>Well, if it's not possible from within MySQLi, maybe I'll stick with something like this:</p>
  
  <pre><code>function preparedQuery($sql,$params) {
    for ($i=0; $i&lt;count($params); $i++) {
      $sql = preg_replace('/\?/',$params[$i],$sql,1);
    }
    return $sql;
  }
  
  $id = 1;
  $baz = 'something';
  
  $sql = "SELECT foo FROM bar WHERE id=? AND baz=?";
  
  echo preparedQuery($sql,array($id,$baz));
  
  // outputs: SELECT foo FROM bar WHERE id=1 AND baz=something
  </code></pre>
  
  <p>Far from perfect obviously, since it's still pretty redundant &mdash; something I wanted to prevent &mdash; and it also doesn't give me an idea as to what's being done with the data by MySQLi. But I guess this way I can quickly see if all the data is present and in the right place, and it'll save me <em>some</em> time compared to fitting in the variables manually into the query &mdash; that can be a pain with many vars.</p>