简体   繁体   English

我可以获取PHP的PDO来返回MySQL警告,而不是仅返回错误消息吗?

[英]Can I get PHP's PDO to return MySQL warnings, instead of only error messages?

I'm trying to get as much information back to my users as possible, and I would like to be able to tell them if there has been a warning from one of the operations they have just performed, for example if data has been truncated because it has been stuffed into a field that is too short, or they've tried to put letters or a decimal into an integer field. 我正在尝试将尽可能多的信息反馈给我的用户,并且我希望能够告诉他们,他们刚刚执行的一项操作是否发出警告,例如,数据是否被截断是因为它已被填充到一个太短的字段中,或者他们试图将字母或小数放入整数字段中。

For testing purposes I have reduced my code to this on a page on it's own: 为了进行测试,我在自己的页面上将代码缩减为:

<?php
        error_reporting(E_ALL);
        ini_set('display_errors', True);
        session_start();
        include '../php/security.php';
        sec::dieifnotvalid();
        include '../php/db_connection_params.php';

        $pdo_conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
        $qry_string = "insert into tbl_engine (capacity) values (?)";
        $var_array = array('gfd');
        $q = $pdo_conn->prepare($qry_string);
        $q->execute($var_array);
        $return_arr = $q->fetchAll();


        var_dump($q->errorInfo());
        ?>

if I try to execute something like this, 如果我尝试执行这样的事情,

$qry_string = "insert into tbl_manufacturer_lookup (manufacturer_name) values (?)";
$var_array = array('Audi');

which would be inserting a duplicate entry into a unique column, I get 将重复项插入到唯一列中,我得到

array(3) { [0]=> string(5) "00000" [1]=> int(1062) [2]=> string(57) "Duplicate entry 'Audi' for key 'manufacturer_name_UNIQUE'" }

printed on the page. 打印在页面上。 which is good. 很好

But if I do something like this, 但是如果我做这样的事情,

    $qry_string = "insert into tbl_engine (capacity) values (?)";
    $var_array = array('hjgt');

which is inserting a string into an integer field, the errorinfo is 这是将字符串插入整数字段,errorinfo是

array(3) { [0]=> string(5) "HY000" [1]=> NULL [2]=> NULL }

which is exactly the same as if I inserted an integer value. 这与插入整数值完全相同。

By comparison, MySQL workbench will return something like this: 相比之下,MySQL工作台将返回以下内容:

1 row(s) affected, 1 warning(s): 1366 Incorrect integer value: 'fdd' for column 'capacity' at row 1

I would really like to get my web app to return something useful like this 我真的很想让我的网络应用返回类似这样的有用信息

Execute SHOW WARNINGS query. 执行SHOW WARNINGS查询。 More on it here . 这里更多。

For those looking, a quick and easy way to do this is with the query SHOW WARNINGS . 对于那些寻找的人来说,一种快速简便的方法是使用查询SHOW WARNINGS Here is an easy function to reuse as needed. 这是一个易于根据需要重用的函数。

function lastWarning($pdo)
{
    var_dump($pdo->query("SHOW WARNINGS")->fetch());
}

You can make this as pretty as you want. 您可以根据需要使它漂亮。 For example, in a PDO child class: 例如,在PDO子类中:

namespace Foo;

class PDO extends \PDO 
{
    public function getWarning()
    {
        $warnings = $this->query('SHOW WARNINGS')->fetch();
        if ($warning['Level'] == 'Error') {
            return "Warning: {$warning['Message']} [#{$warning['Code']}]\n";
        }
    }

}

If I were you I would simple not rely on such warnings. 如果我是你,我将简单地不依赖此类警告。 You should simple in PHP do extra query and validate data and show some warnings (for example for duplicated unique keys) or for fields that should be integer (and they don't). 您应该在PHP中简单地做一些额外的查询和验证数据,并显示一些警告(例如,对于重复的唯一键)或对于应该为整数的字段(而不是整数)。

Otherwise you may insert into database some data that shouldn't be put there (for example 5,52 as decimal would be a problem and maybe you should correct it in PHP or let user to input correct value) 否则,您可能会向数据库中插入一些不应放置的数据(例如,5.52,因为十进制会出现问题,也许您应该在PHP中更正它,或者让用户输入正确的值)

To extract PDO warnings, initialise your PDO object like so: 要提取PDO警告,请如下初始化PDO对象:

$pdo_conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass, array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING
));

Note, this should only be used for debugging purposes, as messages often contain sensitive information. 注意,这仅应用于调试目的,因为消息通常包含敏感信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM