php – 如何在使用PDO时设置SQL模式?

我试图设置sql模式,我无法弄清楚如何使用PDO.我试图在 MySQL中设置传统模式,不允许无效日期.

有人可以帮忙吗?

在运行时设置sql_mode时,可以使用可选的“SESSION”变量.这样它不会影响其他客户.您可以设置SESSION sql_mode,然后在查询完成后将其设置回上一个值.通过这种方式,您可以为特定操作设置sql_mode.

MySql手册:

“You can change the sql mode at runtime by using a SET
[GLOBAL|SESSION] sql_mode=’modes’ statement to set the sql_mode system
value. Setting the GLOBAL variable requires the SUPER privilege and
affects the operation of all clients that connect from that time on.
Setting the SESSION variable affects only the current client. Any
client can change its own session sql_mode value at any time.”

我个人在我的数据库类中添加了一些方法来处理这个问题. initsqlMode()将执行查询’SELECT SESSION.sql_mode’并将认值存储为类变量. setsqlMode()允许您将SESSION sql_mode设置为(VALIDATED)自定义值. resetsqlMode()将SESSION sql_mode设置回认值.我总是在操作sql_mode时使用SESSION变量.

然后你可以做类似以下的事情.注意这只是伪代码;我的例子中没有任何东西可以阻止sql注入或参数化SQL查询.

$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;
     }
}

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...