問題描述
我的網站上有一個頁面(高流量),每次加載頁面時都會插入.
I have a page on my website (high traffic) that does an insert on every page load.
我很好奇最快和最安全的方法(捕獲錯誤)并在系統無法插入 MySQL 時繼續.我應該使用 try/catch 或 die 還是其他什么.我想確保插入發生,但如果由于某種原因它不能,我希望頁面仍然繼續加載.
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);
...
推薦答案
檢查 文檔 表明其出錯時返回 false
.所以使用返回狀態而不是 或 die()
.如果失敗,它將返回 false,您可以記錄(或任何您想做的事情)然后繼續.
Checking the documentation shows that its returns false
on an error. So use the return status rather than or 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 Try and Catch for SQL Insert的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!