PHP PDO::execute()执行insert into失败返回错误无错误消息的解决
2017-06-10 18:50
375 查看
$dsn = 'mysql:host=localhost;dbname=testdb'; $db = new PDO($dsn, 'root', 'password'); $st = $db->prepare('insert into tbl_wh(vid, bid, name, cre_time) values(?,?,?,?)'); $data = array($vid, $bid, $name, $now); try { $result = $st_vol->execute($data); $id = $db->lastinsertid(); echo $id .' result:'.var_export($result,true); } catch (PDOException $e) { echo 'insert error: '.print_r($data,true) ."\n" .$e->getMessage(); }
执行后,查数据库表没有新增。查日志,echo出来的$result是false。但没有捕获到错误消息。
寻求度娘指导,发现需要添加一行代码,设置PDO错误提示模式:
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
修改后完整代码如下:
$dsn = 'mysql:host=localhost;dbname=testdb'; $db = new PDO($dsn, 'root', 'password'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $st = $db->prepare('insert into tbl_wh(vid, bid, name, cre_time) values(?,?,?,?)'); $data = array($vid, $bid, $name, $now); try { $result = $st_vol->execute($data); $id = $db->lastinsertid(); echo $id .' result:'.var_export($result,true); } catch (PDOException $e) { echo 'insert error: '.print_r($data,true) ."\n" .$e->getMessage(); }
再执行,OK,可以捕获到具体的错误消息了。
相关文章推荐
- curl 函数执行了之后返回false解决,打印错误信息返回PHP的Curl出现:curl call error(77): Problem with the SSL CA cert (path? ac
- 错误解决:SharePoint Designer 2010编辑后,出现数据源控件未能执行插入命令,data source control failed to execute the insert command
- OCIEnvCreate 失败, 返回代码为-1,但错误消息文本不可用 解决办法
- OleDbDataAdapter 的Update语句执行时提示“insert into语法错误”解决方法
- OleDbDataAdapter 的Update语句执行时提示“insert into语法错误”解决方法
- php调用存储过程返回结果集,解决can't return a result set in the given context错误的方法
- php调用存储过程返回结果集,解决can't return a result set in the given context错误的方法
- DELPHI INSERT INTO 语句的语法错误 解决方法
- Could not create an environment: OCIEnvCreate returned -1(OCIEnvCreate 失败, 返回代码为-1,但错误消息文本不可用)
- 使用OleDbCommandBuilder时出现“Insert into 语句的语法错误”的解决方法
- win7 homebasic下,.net2008 连接oracle,提示错误OCIEnvCreate 失败,返回代码为 -1,但错误消息文本不可用
- 使用OleDbCommandBuilder时出现“Insert into 语句的语法错误”的解决方法
- 使用OleDbCommandBuilder时出现“Insert into 语句的语法错误”的解决方法
- 安装SQL Server2005时,出现“对性能监视器计数器注册表值执行系统配置检查失败”的错误,如何解决?
- oracle提示 ORA-12154: TNS: 无法解析指定的连接标识符 OCIEnvCreate 失败, 返回代码为-1,但错误消息文本不可用
- oracle提示 ORA-12154: TNS: 无法解析指定的连接标识符 OCIEnvCreate 失败, 返回代码为-1,但错误消息文本不可用
- 使用OleDbCommandBuilder时出现“Insert into 语句的语法错误”的解决方法
- 使用OleDbCommandBuilder时出现“Insert into 语句的语法错误”的解决方法
- “OCIEnvNlsCreate 失败,返回代码为 -1,但错误消息文本不可用”的处理方法收藏
- Could not create an environment: OCIEnvCreate returned -1(OCIEnvCreate 失败, 返回代码为-1,但错误消息文本不可用)