Maison  >  Article  >  développement back-end  >  php pdo sql语句错误为何没报错误信息

php pdo sql语句错误为何没报错误信息

WBOY
WBOYoriginal
2016-06-06 20:29:001411parcourir

<code>$cdb=new \PDO('mysql:dbname=crawler;host=127.0.0.1;charset=utf8','root','*****');
$cdb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

$sql='update jb51_ps_article set is_handle=1 and new_article_id=5 where id=71665';
$state=$cdb->exec($sql);
print_r($cdb->errorInfo());
</code>

这里的update语句的set部分写错了set ishandle=1 and newarticle_id=5

<code>$sql='update jb51_ps_article set is_handle=1 and new_article_id=5 where id=71665';</code>

应该为set ishandle=1,newarticle_id=5

执行后数据表不会更改,但是没有任何错误信息,害得我弄了半天,不知道是怎么回事?

下面是输出信息:
php pdo sql语句错误为何没报错误信息

回复内容:

<code>$cdb=new \PDO('mysql:dbname=crawler;host=127.0.0.1;charset=utf8','root','*****');
$cdb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

$sql='update jb51_ps_article set is_handle=1 and new_article_id=5 where id=71665';
$state=$cdb->exec($sql);
print_r($cdb->errorInfo());
</code>

这里的update语句的set部分写错了set ishandle=1 and newarticle_id=5

<code>$sql='update jb51_ps_article set is_handle=1 and new_article_id=5 where id=71665';</code>

应该为set ishandle=1,newarticle_id=5

执行后数据表不会更改,但是没有任何错误信息,害得我弄了半天,不知道是怎么回事?

下面是输出信息:
php pdo sql语句错误为何没报错误信息

创建PDO对象的时候设置一下错误模式

<code>$cdb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);</code>

然后try/catch一下就可以捕获到错误了

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn