Jump to content
Larry Ullman's Book Forums

How To Output Sql Query For Debugging When Using Prepared Statements?


Recommended Posts

Hi All,

 

I've been beating my head against the wall most of the day trying to get prepared statements to work.  I've walked away from my desk today in frustration more than any day in recent memory!  It seems like prepared statements should be simple but I'm having a heck of a time getting results from the query (no mysql_fetch_array, e.g. - at least not that I can find on php.net).

 

Anyway, my main issue at this point is how do I output the "final" sql query from a prepared statement (that is, with the bound parameters included) in order to debug my queries?

 

Is there a way?

 

Or do I have to retype the query (rather than copy and paste) into a mysql interface to test?

 

Thanks in advance - esp to Larry (I've found your books to be very helpful over the years)

Best,

Adam

Link to comment
Share on other sites

I'm not sure there is an easy way as the query is first sent and the variables are sent later. What exactly is the error you're getting? I often copy and paste the query into something like phpmyadmin and substitute real values for the variables. If that does not produce an error, echo out the variables before binding them to ensure they are valid. Its difficult to be more helpful without more information. Show us the code for the prepared statement and what error you're getting.

Link to comment
Share on other sites

Prepared statements are kind of weird. You have to bind the results to. :)

 

if ( $stmt = $mysqli->prepare("SELECT first_name, last_name FROM person") )
{
    /* execute statement */
    $stmt->execute();

    /* bind result variables */
    $stmt->bind_result($fn, $ln);

    /* fetch values */
    while ($stmt->fetch()) {
        echo $fn. ', ' . $ln. '<br/>';
    }

    /* close statement */
    $stmt->close();
}
Link to comment
Share on other sites

 Share

×
×
  • Create New...