$stmt->execute() :如何知道数据库插入是否成功? [英] $stmt->execute() : How to know if db insert was successful?

查看:154
本文介绍了$stmt->execute() :如何知道数据库插入是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,我怎么知道数据库中插入了任何内容?

With the following piece of code, how do i know that anything was inserted in to the db?

if ($stmt = $connection->prepare("insert into table (blah) values (?)")) {
$stmt->bind_param("s", $blah);  
$stmt->execute();           
$stmt->close();                                 
}

我原以为添加以下行会起作用,但显然不会.

I had thought adding the following line would have worked but apparently not.

if($stmt->affected_rows==-1){$updateAdded="N"; echo "failed";}  

然后使用 $updatedAdded="N" 跳过依赖于上述插入成功的页面下方的其他代码段.

And then use the $updatedAdded="N" to then skip other pieces of code further down the page that are dependent on the above insert being successful.

有什么想法吗?

推荐答案

execute() 方法返回一个 boolean ... 所以就这样做:

The execute() method returns a boolean ... so just do this :

if ($stmt->execute()) { 
   // it worked
} else {
   // it didn't
}

这篇关于$stmt->execute() :如何知道数据库插入是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆