Help Center

How to modify sql_mode or Strict Mode in MySQL?

There are cases where an application or website requires disabling some restriction of the MySQL variable sql_mode.
For example, disabling “ONLY_FULL_GROUP” or “STRICT_TRANS_TABLES”.

Since modifying the sql_mode variable is not allowed on shared hosting servers, you can modify the restrictions with the following PHP code examples:

Remove all “sql_mode” restrictions, in PDO connections:

php
<?php
$dsn = 'mysql:host=localhost;dbname=database_name';
$username = 'username';
$password = 'password';

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // Set SQL_MODE to empty
    $pdo->exec("SET sql_mode = ''");

    // Verify the change
    $stmt = $pdo->query("SELECT @@sql_mode");
    $sql_mode = $stmt->fetchColumn();
    echo "Current SQL_MODE: " . $sql_mode;

} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
?>

Remove only a specific “sql_mode” restriction, in PDO connections:

In the following example we’ll remove only the “ONLY_FULL_GROUP_BY” restriction.

php
/<?php
$dsn = 'mysql:host=localhost;dbname=database_name';
$username = 'username';
$password = 'password';

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // Remove ONLY_FULL_GROUP_BY from SQL_MODE
    $pdo->exec("SET SESSION sql_mode = sys.list_drop(@@SESSION.sql_mode, 'ONLY_FULL_GROUP_BY')");

    // Verify the change
    $stmt = $pdo->query("SELECT @@SESSION.sql_mode");
    $sql_mode = $stmt->fetchColumn();
    echo "Current SQL_MODE: " . $sql_mode;

} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
?>
Guide g.0210 · Source version 1 · en