Home  >  Article  >  Backend Development  >  php pdo sql语句错误为何没报错误信息

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

WBOY
WBOYOriginal
2016-06-06 20:29:001409browse

<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一下就可以捕获到错误了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn