How to Print the Actual Query That MySQLi->Execute() Makes

Is there any way to print the actual query that mysqli-execute() makes?

If your statement is failing, you should check $stmt->error (as opposed to $dbi->error). As far as getting the actual text of the query: it's not possible. When using prepared statements, the library is using a special protocol that doesn't generate an actual query string for each ->execute() call.

How to echo a MySQLi prepared statement?

I don't think you can - at least not in the way that you were hoping for. You would either have to build the query string yourself and execute it (ie without using a statement), or seek out or create a wrapper that supports that functionality. The one I use is Zend_Db, and this is how I would do it:

$id = 5;
$baz = 'shazam';
$select = $db->select()->from('bar','foo')
->where('id = ?', $id)
->where('baz = ?', $baz); // Zend_Db_Select will properly quote stuff for you
print_r($select->__toString()); // prints SELECT `bar`.`foo` FROM `bar` WHERE (id = 5) AND (baz = 'shazam')

Print the Result of a mysqli SELECT Query

Use Fetch to display the result

     $result = mysqli_query($con,"SELECT * FROM user_list where username = '" . mysqli_real_escape_string($con, $username) . "'"); 
while($row = mysqli_fetch_array($result))
{
print_r($row);
}

How can I view the contents of a prepared statement?

Using prepared statements:

  • When you prepare the statement, it is sent to the MySQL server
  • When you bind the variables + execute the statement, only the variables are sent to the MySQL server
  • And the statement + bound variables are executed on the MySQL server -- without it re-doing the "preparation" each time the statement is executed (which is why prepared statements can be good for performance when the same statement is executed several times)

There is no "building" of an SQL query on the PHP side, so, there is no way to actually get that query.

Which means that if you want to see an SQL query, you have to use, well, SQL queries, and not prepared statements.



Related Topics



Leave a reply



Submit