doutuo3935 2012-03-25 05:02
浏览 46
已采纳

使用php处理mysql错误的正确方法是什么?

What is the "proper" way to deal with errors when manipulating a sql database with php?

What Im currently doing looks like this:

$connection = new mysqli('hostname', 'user', 'pass', 'database');

if ($connection->connect_errno) {
    reportError("DB_CONNECTION_ERROR", $connection->connect_errno, $connection->connect_error);
displayError("DB_CONNECTION_ERROR");
}

$stmt = $connection->stmt_init();
$q = "query";

$stmt->prepare($q);
$stmt->bind_param('s', $username);
$stmt->execute();

reportError() is part of an error handling file I wrote and logs the error in a database

displayError() is part of the same file and tells the page what to display (as opposed to displaying the actual error).

However Im not sure of how to check for other errors, such as whether a statement was successfully prepared or whether a query was successful. Any recommendations appreciated!

  • 写回答

2条回答 默认 最新

  • douzha5990 2012-03-25 05:08
    关注

    Don't you find it quite odd to write database connection errors... into database?

    I see also no point in having custom displayError() function. It should be generic _503() function, sending corresponding header along with general excuses.

    I see no point in having custom logError() function either. PHP quite capable to log errors itself. trigger_error() serves me best.

    Im not sure of how to check for other errors, such as whether a statement was successfully prepared

    Ah, this one. Exceptions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)