When setting sql_mode at run time, you can use the optional "SESSION" variable. Therefore, this will not affect other customers. You can set SESSION sql_mode and then return it to the previous value after the query is completed. This way you can set sql_mode for a specific operation.
From the MySql manual:
"You can change the SQL mode at run time using SET [GLOBAL | SESSION] sql_mode = 'modes' to set the sql_mode system cost. To set the GLOBAL variable, the SUPER privilege is required and affects the operation of all clients that connect from now on. Setting the variable SESSION only affects the current client. Any client can change their sql_mode session value at any time. "
I personally added some methods to my database class to handle this. initSqlMode () will execute the query "SELECT SESSION.sql_mode" and save the default value as a class variable. setSqlMode () will allow you to set SESSION sql_mode to a (VALIDATED) custom value. resetSqlMode () sets the default SESSION sql_mode. I use the SESSION variable when working with sql_mode anytime.
Then you can do something like the following. Note that this is only psuedocode; in my example, there is nothing to prevent SQL injection or parameterize the SQL query.
$db = new database(); $badqueryresult = $db->executeStrict('BAD SQL QUERY'); Class database { ... function executeStrict($query){ $this->initSqlMode(); $this->setSqlMode('STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'); $result = $this->Execute($query); $this->resetSqlMode(); return $result; } }
source share