Version: v2.4.0
debug
Enable debug mode to print the generated SQL without executing it.
debug()
Return Value
[Medoo] The Medoo instance with debug mode enabled.
Medoo outputs the generated SQL without executing it. Disable debug mode after troubleshooting.
Generated SQL
Use
debug() to inspect the SQL generated by the current query.$database->debug()->select("bccount", [
"user_name",
"email"
], [
"user_id[<]" => 20
]);
// Output:
// SELECT "user_name","email" FROM "bccount" WHERE "user_id" < 20
Dangerous Case
In some cases, a single request executes multiple SQL queries. If you call
debug() on one query, the subsequent code still runs, which may lead to unexpected behavior.// This outputs nothing.
$database->insert("account", [
"user_name" => "foo",
"email" => "foo@bar.com"
]);
// Outputs the generated query.
$post_id = $database->debug()->get("post", "post_id", ["user_name" => "foo"]);
// Caution: this query will execute. Because debug() ran above, $post_id is now false.
$database->update("account", [
"level[+]" => 5,
"post" => $post_id
], [
"user_name" => "foo"
]);
Debug Logging
To debug your project without adding
debug() to every query, call beginDebug() to start logging. Then call debugLog() to stop logging and return all collected SQL statements as an array.// Start debug logging.
$database->beginDebug();
// These queries are not executed; they are recorded in the debug log.
$database->select("account", ["user_name", "email"], ["user_id[<]" => 20]);
$database->update("account", ["level[+]" => 1], ["user_id[>]" => 100]);
// Call debugLog() to stop logging and return all logged SQL statements.
var_dump($database->debugLog());
// Output:
array(2) {
[0]=> string(10) "SELECT ... FROM"
[1]=> string(10) "UPDATE ... SET ... WHERE ..."
}