douxing1850 2012-09-28 16:08
浏览 157
已采纳

如何使用PDO(mysql)正确调试Insert语句?

$PDO_db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=UTF-8' , DB_USER, DB_PWD);
$sth = $PDO_db->prepare($insert);   
$arrPar = array(':nome' =>  $nome);         
$r = $sth->execute($arrPar);    
var_dump($arrPar);
print_r($PDO_db->errorInfo());
var_dump($r);
exit();

Supppose that the $insert statemet contains a SQL error, with the code above I've noticed the following :

  1. Code A

    print_r($PDO_db->errorInfo());
    

    will output anyways:

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

    but var_dump($s) is false

  2. If in the insert statement I have some placemark e.g. :name

    "Insert into mytable (name) VALUE(:name);"
    

    And then in the $arrPar = array(':name' => $value) with $value=NULL

    print_r($PDO_db->errorInfo());
    

    will output again:

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

    but var_dump($s) is false

So I'm wondering to know what's the correct way to debug MySQL statements with PDO if the method errorInfo, which is supposed to give me a error description, silently quiets errors?

PDO::errorInfo() returns an array of error information"

  • 写回答

1条回答 默认 最新

  • doumi4676 2012-09-28 16:27
    关注

    Strange... are you sure nothing is being iserted? MySQL's return code on success is 0, which the errorInfo does return. But like @Jack said:

    try
    {
        $PDO_db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=UTF-8' , DB_USER, DB_PWD);
        $PDO_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sth = $PDO_db->prepare($insert);
        echo 'Insert prepared<br/>';
        $arrPar = array(':nome' =>  $nome);//shouldn't this be :name?
        $r = $sth->execute($arrPar);
        echo 'Insert executed<br/>';
        var_dump($r);
    }
    catch(PDOException $pdoE)
    {
        echo $pdoE->getMessage().'<br/>';
        var_dump($pdoE);
    }
    exit();//?
    

    That should give you more clues as to what is going on. Also, you state (twice) that a var_dump of $s is false, but you're assigning the return value of the execute call to $r... I'm assuming that's a typo, but you never know, hence the comment on ':nome'.
    Also take @Clarence's advice to heart, and set your php.ini error reporting to E_ALL | E_STRICT, you might hate the vast amount of warnings it issues at first, but it avoids any issues you might (and probably will) encounter when you deploy your code on another machine, or you upgrade your PHP version. Anyway: it's not like PHP is that strict of a language that E_STRICT is going to cost you huuuge amounts of time to fix the warnings...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法