How can I view the contents of a prepared statement?

后端 未结 6 2061
慢半拍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条回答
  •  萌比男神i
    2020-12-06 16:52

    • You can use PDOStatement->debugDumpParams to get some informations about the prepared statement (in case you're using pdo).
    • Prepared statements are logged in MySQL's general log:
    For prepared statements that are executed with the mysql_stmt_prepare() and mysql_stmt_execute() C API functions, the server writes Prepare and Execute lines to the general query log so that you can tell when statements are prepared and executed.
    [...] the server writes the following lines to the general query log:
    Prepare [1] SELECT ?
    Execute [1] SELECT 3

    So for debugging purposes active the general log and keep an eye on that file.

    edit: oh, the question has a [mysqli] tag... completely overlooked that.
    If the statement isn't executed at all have you (double/tripple) checked that no error occurred along the way?

    echo "
    Debug: start
    \n"; $mysqli = new mysqli('localhost', 'localonly', 'localonly', 'test'); if ($mysqli->connect_error) { die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); } $result = $mysqli->query('CREATE TEMPORARY TABLE foo (id int auto_increment, x int, primary key(id))'); if ( false=== $result) { die('error : '. $mysqli->error); } $stmt = $mysqli->prepare('INSERT INTO foo (x) VALUES (?)'); if ( false===$stmt ) { die ('prepare() failed: ' . $mysqli->error); } $result = $stmt->bind_param('i', $x); if ( false===$result ) { die('bind_param() failed'); } $x = 1; $result = $stmt->execute(); if ( false===$result ) { die('execute() failed: '.$stmt->error); } echo "
    Debug: end
    \n";

提交回复
热议问题