execute($arr);if ($t"/> execute($arr);if ($t">

Home >Backend Development >PHP Tutorial >pdo用prepare执行insert,增加记录失败,但捕获不到异常

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

WBOY
WBOYOriginal
2016-06-13 12:22:491298browse

pdo用prepare执行insert,增加记录失败,但捕获不到错误
$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());


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

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