How can I view the contents of a prepared statement?

后端 未结 6 2071
慢半拍i
慢半拍i 2020-12-06 15:54

I\'m working on learning to use prepared statements with mysqli in PHP and usually, if I\'m having a problem with a query I just echo it to the screen to see what it looks l

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 16:30

    I usually do this when I need to debug a prepared sql with parameters.

    Example of prepare and execute:

    $sql = "SELECT VAL1, VAL2 FROM TABLE(?, '?', '?', '?', '?', ?, '?', '?', '?')";
    $prep = ibase_prepare( $sql ) or die("Error");
    $query = ibase_execute($prep, $param1, $param2, .....) or $err = true;
                                  ^^^^^^^^^^^^^^^^^^^^^^^
    

    The easy way to debug the resulting SQL of the sentence it's:

    printf( str_replace('?', '%s', $sql), $param1, $param2, ....);
                                          ^^^^^^^^^^^^^^^^^^^^^^
    

    You only need to do one printf, replacing the ? on the prepared SQL string by one %s. printf will interpret all as one string, taking each parameter as placing it on each replaced %s.

提交回复
热议问题