6

Is there any way to get the actual SQL that is the result of preparing a statement when using the mysqli extension?

My problem: I am using prepared statements. In all cases, they SHOULD update a record. I am not catching any errors. However, when I check for affected rows, there are none. So, I want to see the actual SQL that would be executed as a result of the prepare statement.

Is there any way to do this? I've check the mysqli reference docs, but they don't seem to have anything.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Justin Noel
  • 5,945
  • 10
  • 44
  • 59

1 Answers1

0

While I was looking for a Mysqli specific answer I found that all refer to PDO only thus not addressing the issue with using the ? placeholder.

So here I post my contribution of a simple function to replace all the ? for the parameters. (PHP 7)

function addParamsToSQL ( string $SQL, array $params ) : string {

    foreach($params as $value)
        $SQL = preg_replace ( '[\?]' , "'" . $value . "'" , $SQL, 1 );

    return $SQL;
}
JDuarteDJ
  • 1,073
  • 12
  • 25