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

WBOY
Release: 2016-06-06 20:29:00
Original
1408 people have browsed it

<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>
Copy after login
Copy after login

这里的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>
Copy after login
Copy after login

应该为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>
Copy after login
Copy after login

这里的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>
Copy after login
Copy after login

应该为set ishandle=1,newarticle_id=5

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

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

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

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!