PHP尝试并抓住SQL插入 [英] PHP Try and Catch for SQL Insert
问题描述
我在我的网站上有一个页面(流量很高),在每个页面上都插入一个页面。
我很好奇最快最安全的方式一个错误),如果系统不能插入到MySQL中,继续。我应该使用try / catch或die或其他的东西。我想确保插入发生,但如果由于某种原因,我不能让页面继续加载。
...
$ db = mysql_select_db('mobile',$ conn);
mysql_query(INSERT INTO redirects SET ua_string ='$ ua_string')或die('Error#10');
mysql_close($ conn);
...
检查文档显示它在错误上返回 false
。所以使用返回状态而不是或die()
。它将返回false,如果失败,您可以登录(或任何你想要做的),然后继续。
$ rv = mysql_query(INSERT INTO redirects SET ua_string ='$ ua_string');
/ pre>
if($ rv === false){
//处理这里的错误
}
//页面继续加载
I have a page on my website (high traffic) that does an insert on every page load.
I am curious of the fastest and safest way to (catch an error) and continue if the system is not able to do the insert into MySQL. Should I use try/catch or die or something else. I want to make sure the insert happens but if for some reason it can't I want the page to continue to load anyway.
... $db = mysql_select_db('mobile', $conn); mysql_query("INSERT INTO redirects SET ua_string = '$ua_string'") or die('Error #10'); mysql_close($conn); ...
解决方案Checking the documentation shows that its returns
false
on an error. So use the return status rather thanor die()
. It will return false if it fails, which you can log (or whatever you want to do) and then continue.$rv = mysql_query("INSERT INTO redirects SET ua_string = '$ua_string'"); if ( $rv === false ){ //handle the error here } //page continues loading
这篇关于PHP尝试并抓住SQL插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!