PDO mysql:如何知道插入是否成功
我正在使用 PDO 插入一条记录(mysql 和 php)
$stmt->bindParam(':field1', $field1, PDO::PARAM_STR);
$stmt->bindParam(':field2', $field2, PDO::PARAM_STR);
$stmt->execute();
有没有办法知道它是否插入成功,例如记录是否因为重复而未插入?
编辑:我当然可以查看数据库,但我的意思是程序化反馈。
I'm using PDO to insert a record (mysql and php)
$stmt->bindParam(':field1', $field1, PDO::PARAM_STR);
$stmt->bindParam(':field2', $field2, PDO::PARAM_STR);
$stmt->execute();
Is there a way to know if it inserted successfully, for example if the record was not inserted because it was a duplicate?
Edit: of course I can look at the database, but I mean programmatic feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
PDOStatement->execute()
返回正确的成功。还有PDOStatement->errorCode()
您可以检查错误。PDOStatement->execute()
returns true on success. There is alsoPDOStatement->errorCode()
which you can check for errors.鉴于最推荐的 PDO 错误模式是
ERRMODE_EXCEPTION
,直接execute()
结果验证将无法工作。因为代码执行甚至无法达到其他答案中提供的条件。因此,在 PDO 中处理查询执行结果有以下三种可能的情况:
try..catch
运算符。对于普通 PHP 用户来说,这听起来有点陌生 - 不验证操作的直接结果怎么样? - 但这正是异常的工作原理 - 您在其他地方检查错误。一次就完事了。极其方便。
所以,简而言之:在常规代码中,您根本不需要任何错误处理。只需保持你的代码不变:
成功时它会告诉你,出错时它会向你显示你的应用程序在这种情况下显示的常规错误页面。
仅当您有处理场景而不仅仅是报告错误时,请将插入语句放入
try..catch
运算符中,检查这是否是您预期的错误,并处理它;或者 - 如果错误有任何不同 - 重新抛出异常,以便可以通过站点范围的错误处理程序通常的方式进行处理。下面是我的关于使用 PDO 进行错误处理的文章中的示例代码:在上面的代码中,我们正在检查采取某些操作的特定错误,并为任何其他错误(例如没有这样的表)重新抛出异常,这将报告给程序员。
再次强调 - 只是告诉用户“您的插入成功”之类的内容不需要任何条件。
Given that most recommended error mode for PDO is
ERRMODE_EXCEPTION
, no directexecute()
result verification will ever work. As the code execution won't even reach the condition offered in other answers.So, there are three possible scenarios to handle the query execution result in PDO:
try..catch
operator.For a regular PHP user it sounds a bit alien - how's that, not to verify the direct result of the operation? - but this is exactly how exceptions work - you check the error somewhere else. Once for all. Extremely convenient.
So, in a nutshell: in a regular code you don't need any error handling at all. Just keep your code as is:
On success it will tell you so, on error it will show you the regular error page that your application is showing for such an occasion.
Only in case you have a handling scenario other than just reporting the error, put your insert statement in a
try..catch
operator, check whether it was the error you expected and handle it; or - if the error was any different - re-throw the exception, to make it possible to be handled by the site-wide error handler usual way. Below is the example code from my article on error handling with PDO:In the code above we are checking for the particular error to take some action and re-throwing the exception for the any other error (no such table for example) which will be reported to a programmer.
While again - just to tell a user something like "Your insert was successful" no condition is ever needed.
尝试查看
execute
的返回值,成功时为TRUE
,失败时为FALSE
。Try looking at the return value of
execute
, which isTRUE
on success, andFALSE
on failure.如果更新查询使用与当前数据库记录匹配的值执行,则
$stmt->rowCount()
将返回0
,因为没有行受到影响。如果您有一个 if( rowCount() == 1 ) 来测试是否成功,您会认为更新失败,因为它没有失败,但值已经在数据库中,因此没有任何变化。当我尝试使用违反的唯一键字段更新记录时,这对我不起作用。查询返回成功,但另一个查询返回旧字段值。
If an update query executes with values that match the current database record then
$stmt->rowCount()
will return0
for no rows were affected. If you have anif( rowCount() == 1 )
to test for success you will think the updated failed when it did not fail but the values were already in the database so nothing change.This did not work for me when I tried to update a record with a unique key field that was violated. The query returned success but another query returns the old field value.
您可以测试行数
You can test the rowcount
PDOStatement->execute() 会抛出异常,
所以你能做的是
PDOStatement->execute() can throw an exception
so what you can do is
使用 id 作为主键并自动递增
增量 id 始终大于零,即使在第一条记录上也是如此,这意味着它将始终返回 id 的真值,因为大于零在 PHP 中意味着 true
Use id as primary key with auto increment
incremental id is always bigger than zero even on first record so that means it will always return a true value for id coz bigger than zero means true in PHP