插入到唯一列时捕获错误并继续
我有一个独特的专栏。据我了解,问题是,当您尝试向该列插入一些违反其唯一性的数据时,查询将停止处理。我想做的只是捕获错误并继续处理其余行。您将如何使用 PHP 和 MySQL 以最有效的方式做到这一点?
这里有一个简短的例子 http://pastie.org/1357001
我真正想知道的是,如果发生错误,循环会被破坏吗?
I have a unique column. The thing is, as I understand it, when you try to insert some data into that column that violates its uniqueness, the query will stop getting processed. What I wanna do is simply capture the error and continue with the rest of the rows. How would you do this in the most efficient way using PHP and MySQL?
here you have a short example
http://pastie.org/1357001
what i really was wondering is that will the loop get broken if an error occurs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这一点上我和 ajreal 是一致的。违反约束不应导致 PHP 中出现异常。您可以只测试 mysqli_stmt::execute 的返回值,或您正在使用的任何东西,并处理发生的任何错误:
打印:
I'm with ajreal on this one. Violating a constraint should not cause an exception in PHP. You can just test the return value from mysqli_stmt::execute, or whatever it is that you are using, and deal with any errors that have occurred:
Prints:
您应该能够捕获该错误:
You should be able to catch that error:
例外是必经之路。
Exceptions are the way to go.