سؤال

I am connecting to a MySQL database with the following using PDO.

    try {
        $this->myDBH = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass);
        $this->myDBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (Exception $e) {
        $cameralife->error('Database error: ' . htmlentities($e->getMessage()));
    }

Can I enable a "strict" SQL-99 compliance mode that will reject any query that does not follow that standard?

هل كانت مفيدة؟

المحلول

You can specify the mysql sql_mode with a query like

SET SESSION sql_mode = 'xxx'

where xxx is one of the valid sql modes.

There is an ansi sql mode, which makes MySQL behave more like a standard compliant DBMS. Although it is just "more like" not "as".

You can read about the possible sql modes in the manual. The combined sql modes are the interesting ones.

There is also a manual page dedicated to standards compliance, which names sql_mode="ansi" as one option.

I do not know of a way to achieve something similar with pure PDO and/or without this setting.

نصائح أخرى

Nope. PDO does not parse, enforce, or rewrite SQL syntax. Except for allowing named query parameters, which are not SQL-99 standard.

As @GhostGambler answered, MySQL has an SQL_MODE variable that lets us enable a few features of syntax and semantics to conform to SQL-99 a bit better, but this is incomplete.

Even when SQL_MODE=ANSI, MySQL still allows many MySQL-specific features that are not SQL-99. For example:

  • REPLACE
  • INSERT...ON DUPLICATE KEY UPDATE
  • INSERT...SET column=value
  • LIMIT clause
  • Multi-table joined UPDATE and DELETE
  • Most of the built-in SQL functions
  • CREATE TABLE options
  • Data types like SET and ENUM
  • etc.

You wouldn't want to reject any query that doesn't follow SQL-99 anyway. For instance, that would mean you wouldn't be able to CREATE INDEX (indexes in general, and any statements needed to manage them, are left as an implementation detail, not specified by the SQL standard).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top