处理PDO :: ATTR_EMULATE_PREPARES属性为false不会在存储的函数中引起错误(MySql)

时间:2018-10-11 06:55:07

标签: php mysql pdo mariadb

如果将PDO :: ATTR_EMULATE_PREPARES属性设置为false,则不会引发Mysql存储功能的无效参数错误。

  • CentOS 6.10,PHP 7.2.9,MariaDB 10.2.11
  

MySQL存储功能(示例)

CREATE DEFINER=`tester`@`%` FUNCTION `blabla`(`item` TEXT, `no` INT, `option` TINYINT, `type` VARCHAR(1))....
  

PDO :: ATTR_EMULATE_PREPARES = false

<?php
$dbh = new PDO('mysql:......
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$sql = "SELECT blalba('test', 1, 'a')"; // <-- set invalid argument, expected 4
$stmt = $dbh->prepare($sql);
$stmt->execute();
print_r($stmt->errorInfo);

// result ==> 
Array
(
    [0] => 00000
    [1] => 
    [2] => 
)
  

PDO :: ATTR_EMULATE_PREPARES = true

<?php
$dbh = new PDO('mysql:......
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$sql = "SELECT blalba('test', 1, 'a')"; // <-- set invalid argument, expected 4
$stmt = $dbh->prepare($sql);
$stmt->execute();
print_r($stmt->errorInfo);

// result ==> 
Array
(
    [0] => 42000
    [1] => 1318
    [2] => Incorrect number of arguments for FUNCTION project.BLABLA; expected 4, got 3
)

set => false set => true

0 个答案:

没有答案