I am trying to set SQL modes, I can't figure out how to do it using PDO. I am trying to set Traditional mode in MySQL and not allow invalid dates.
Can anyone help?
You can set the SQL_MODE either from the command line (the --sql-mode option) or by setting the sql_mode system variable. SET sql_mode = 'modes'; SET GLOBAL sql_mode = 'modes'; The session value only affects the current client, and can be changed by the client when required.
To set the SQL mode at server startup, use the --sql-mode=" modes " option on the command line, or sql-mode=" modes " in an option file such as my. cnf (Unix operating systems) or my. ini (Windows). modes is a list of different modes separated by commas.
To execute an SQL statement that returns one or more result sets, call the PDO::query method on the PDO connection object, passing in a string that contains the SQL statement. For example, you might want to call this method to execute a static SELECT statement.
The default SQL mode in MySQL 8.0 includes these modes: ONLY_FULL_GROUP_BY , STRICT_TRANS_TABLES , NO_ZERO_IN_DATE , NO_ZERO_DATE , ERROR_FOR_DIVISION_BY_ZERO , and NO_ENGINE_SUBSTITUTION .
This applies to only your connection. The command will be run as soon as PDO connects:
$pdo = new PDO(
$dsn,
$username,
$password,
array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="TRADITIONAL"')
);
See PHP: MySQL (PDO)
See 5.1.6. Server SQL Modes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With