php如何捕获mysql异常,如主键重复插入错误

李士令 2013-08-18 07:44:33
因为在使用uchome的老框架,开发中发现对于重复插入主键数据时mysql报错,但是try catch获取不到异常(特意测试使用try catch抛异常没问题),我想知道如何可以获取到这种数据库错误的异常,跪求解决办法!!!!!!
...全文
1236 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
码无边 2013-08-19
  • 打赏
  • 举报
回复
主键重复,php会提示出来的,不需要人为去控制。
jdgdf566 2013-08-19
  • 打赏
  • 举报
回复
主键重复,一般来说不应该依靠触发错误这个思路设计程序吧,应该先检查主键,甚至重新考虑设计模型
一起混吧 2013-08-18
  • 打赏
  • 举报
回复
主键重复了的话自己会报错了吧
LuciferStar 2013-08-18
  • 打赏
  • 举报
回复


mysql_errno
(PHP 3, PHP 4, PHP 5)

mysql_errno -- Returns the numerical value of the error message from previous MySQL operation
Description
int mysql_errno ( [resource link_identifier] )


Returns the error number from the last MySQL function. 

Errors coming back from the MySQL database backend no longer issue warnings. Instead, use mysql_errno() to retrieve the error code. Note that this function only returns the error code from the most recently executed MySQL function (not including mysql_error() and mysql_errno()), so if you want to use it, make sure you check the value before calling another MySQL function. 
ExSystem 2013-08-18
  • 打赏
  • 举报
回复
或者用mysql的,查warnings
ExSystem 2013-08-18
  • 打赏
  • 举报
回复
用mysqli吧:

$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_STRICT;

try {
    $result = $mysqli->query("。。。");
    $result->close();
    $mysqli->close();
} catch (mysqli_sql_exception $e) {
    //。。。
}

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧