Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the SQL mode while using PDO?

Tags:

php

mysql

pdo

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?

like image 346
M. of CA Avatar asked Aug 07 '11 19:08

M. of CA


People also ask

How do I enable SQL mode?

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.

How do I switch to SQL mode in Linux?

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.

What method function do you use to run an SQL query using a PDO object?

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.

What is the default SQL mode in transactional storage engines?

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 .


1 Answers

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

like image 108
Brian Papantonio Avatar answered Oct 08 '22 01:10

Brian Papantonio