duanji1899 2015-05-03 17:39
浏览 127
已采纳

即使我设置了错误处理,PHP也不会显示PDO错误

I need to insert a row into a database using PDO. But it doesn't work and it doesn't print any errors so I don't know where the problem can be.

The connection is okay, because I can run UPDATE queries.

But on this query it gets stuck without printing any error:

$dbtype     = "mysql";
$dbhost     = "xxx";
$dbname     = "xxx";
$dbuser     = "xxx";
$dbpass     = "xxx";

$db = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "INSERT INTO users (id, name) VALUES (:id,:name)";
$q = $db->prepare($sql);

$q->execute(array(
    ':id'=>$id,
    ':name'=>$name
));

Is there any other way to debug except this command?

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  • 写回答

2条回答 默认 最新

  • dtpa98038 2015-05-03 17:55
    关注

    Probably you have error reporting turned off and you are not trying to catch an exception if your code throws one.

    So you have to put your code into a try and catch block e.g.

    try {
        //your stuff
    } catch(PDOException $e) {
        echo $e->getMessage();
    }
    

    Because your PDO is set to throw an exception if an error occurred, so you have to catch that with this block. Also I would recommend you to turn on error reporting for useful error messages (Only while staging never in production).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分