pdo用prepare执行insert,增加记录失败,但捕获不到错误

WBOY
Release: 2016-06-23 13:30:59
Original
1281 people have browsed it

$st = $this->pdo->prepare("insert into $tablename ($field) values($code)");
$st->execute($arr);
if ($this->pdo->errorCode() != '00000')test($this->pdo->errorInfo());
return $this->pdo->lastInsertId();

故意让一个不能为空的字段给一个空值,然后上面的代码执行后获取不到错误(也输出过errorCode() 是= '00000'),但pdo->lastInsertId()返回的是0,打开数据库去看也是没有成功新增记录,这种PDO预处理的SQL该怎么获取错误提示?


回复讨论(解决方案)

if ($st->errorCode() != '00000') test($st->errorInfo());
Copy after login


或 $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
后,使用异常处理
try {  ....  $st->execute($arr);  ....} catch (PDOException $e) {  print "Error!: " . $e->getMessage() . "\n";}
Copy after login

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