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

查看:143
本文介绍了$ 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天全站免登陆