doumu8911 2014-05-08 21:45
浏览 753
已采纳

如何在MySQLi(PHP)中捕获错误并继续执行?

I have the following class:

    <?php
    require 'config.php';
    ini_set('display_errors', 0);

    abstract class Conexion {

        protected $conexion;

        function __construct() {

        }

        function __destruct() {
            // $this->close();
        }

        protected function abrirConexion() {
            try {
                $this->conexion = new mysqli(BD_HOST, BD_USUARIO, BD_CLAVE, BD_BASEDEDATOS, BD_PUERTO, BD_SOCKET);
                if ($this->conexion->errno == 0) {
                    $this->conexion->query("SET NAMES utf8");
                    return true;
                } else {
                    $this->cerrarConexion();
                    return false;
                }
            } catch (Exception $e) {
                return false;
            }
        }

        protected function cerrarConexion() {
            $this->conexion->close();
        }

    }

and I want to catch the errors and exceptions that may occur in the execution of the script (exactly the connection to the database), and return true if all goes well, or false otherwise. But I could not catch connection errors (For example, when too many connections is generated, the script stops and does not return false). I read about set_handler_error, but I do not know how to implement it.

Can anyone help me?

  • 写回答

1条回答 默认 最新

  • dongyiyu882684 2014-05-09 14:14
    关注

    You can't check $conn->errno after connect, there is a special property $conn->connect_errno for that.

    Also in PHP, when you don't want any errors or warnings to be generated (e.g. because you don't care, or because you are handling them yourself like in your case), you are supposed to use the @ error control operator.

    So it would look like this:

    $this->conexion = @new mysqli(BD_HOST, BD_USUARIO, ...);
    if ($this->conexion->connect_errno == 0) {
        ...
    

    But that means you are doing the checks yourselves and you are not using exceptions at all! It's classic explicit error handling.


    Now let's see how to make it work with exceptions like you wanted. The solution to that is to set mysqli error report mode properly:

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 
    

    As explained in the documentation, this makes mysqli "throw mysqli_sql_exception for errors instead of warnings". You don't need the @ operator then, neither the check for errno value:

        function abrirConexion() {
            // This needs to be set just once.
            mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
            try {
                $this->conexion = new mysqli(...);
                $this->conexion->query("SET NAMES utf8");
                return true;
            } catch (Exception $e) {
                return false;
            }
        }
    

    Simple and clean.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler