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 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题