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 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)