Try / catch in mysqli

后端 未结 3 1098
北海茫月
北海茫月 2021-01-17 22:00

I\'m following an OOP mysqli course. When connecting to the database, they use the following script:

$db = new mysqli(\"host\", \"user\", \"password\", \"dat         


        
3条回答
  •  一生所求
    2021-01-17 22:32

    If you need to catch exceptions on mysqli extension by use try/catch block, try this code (switch on exception mode instead of classic error reporting):

    // Method 1:
    $driver = new mysqli_driver();
    $driver->report_mode = MYSQLI_REPORT_STRICT | MYSQLI_REPORT_ERROR;
    
    // OR Method 2:
    mysqli_report(MYSQLI_REPORT_STRICT | MYSQLI_REPORT_ALL);
    
    try {
        $db = new mysqli("host", "user", "password", "database");
    } catch (mysqli_sql_exception $e) {
        ...
    }
    

    mysqli_sql_exception mysqli_driver

提交回复
热议问题