Debugging prepared statements in php

Prepared statements in PHP sometimes reports a block error which is hard to debug. So for debugging prepared statements we use MYSQLI_REPORT_ALL in conjunction with MySql drivers to get a full detailed message. For using this we have to put our php/mysql code into try block and then the catch exception block will display related error.

Example

// YOUR MYSQL CONNECTION DETAILS HERE

/* activate reporting */
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ALL;

try { // START TRY BLOCK

	$theQuery = "insert into order (TimeAndDate, Type, Amount, Address, status) values (?,?,?,?,?)";
	$stmt = $mysqli->prepare ( $theQuery );

	$stmt->bind_param ( 'ssdsd', $DateTime, $Type, $finalPrice, $Address, $Status );

	$stmt->execute ();
	printf ( "%d Row inserted.\n", $stmt->affected_rows ); 

	// END TRY BLOCK

} 
catch (mysqli_sql_exception $e) { // START CATCH BLOCK

	echo $e->__toString(); // ERROR PRINTING

}
Share

You may also like...