PHP Try and Catch for SQL Insert(PHP Try and Catch for SQL Insert)
问题描述
我的网站上有一个页面(高流量),每次加载页面时都会插入.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP Try and Catch for SQL Insert


- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01