PHP--PDO errors and error handling

伊谢尔伦
Release: 2016-11-22 09:32:17
Original
968 people have browsed it

PDO provides three different error handling modes to meet different styles of application development:

PDO::ERRMODE_SILENT This is the default mode. PDO will simply set the error code, which can be checked using the PDO::errorCode() and PDO::errorInfo() methods on statements and database objects. If the error occurs due to a call to a statement object, then that object's PDOStatement::errorCode() or PDOStatement::errorInfo() method can be called. If the error is caused by calling a database object, then the above two methods can be called on the database object.

PDO::ERRMODE_WARNING In addition to setting the error code, PDO will also send a traditional E_WARNING message. This setting is useful during debugging/testing if you just want to see what's going wrong without interrupting the flow of your application.

PDO::ERRMODE_EXCEPTION In addition to setting the error code, PDO will also throw a PDOException exception class and set its properties to reflect the error code and error information. This setting is also very useful during debugging, as it effectively zooms in on the point in the script where the error occurred, allowing you to very quickly pinpoint potential problematic areas in the code (remember: if an exception causes the script to terminate, the transaction is automatically rolled back roll).

Another very useful thing about exception mode is that you can build your own error handling more clearly than traditional PHP style warnings, and compared to silent mode and explicitly checking the return value of each database call, exception mode requires Less code/nesting.

PDO uses SQL-92 SQLSTATE to standardize error code strings; different PDO drivers are responsible for mapping their native codes to the appropriate SQLSTATE codes. The PDO::errorCode() method returns a single SQLSTATE code. If you need more detailed information about this error, PDO also provides a PDO::errorInfo() method to return an array containing the SQLSTATE code, the specific driver error code, and the error string of this driver.

Example #1 Create a PDO instance and set the error mode

<?php
    $dsn = &#39;mysql:dbname=testdb;host=127.0.0.1&#39;;
    $user = &#39;dbuser&#39;;
    $password = &#39;dbpass&#39;;
    try {
        $dbh = new PDO($dsn, $user, $password);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        echo &#39;Connection failed: &#39; . $e->getMessage();
    }
?>
Copy after login

Regardless of whether PDO::ATTR_ERRMODE is currently set, if the connection fails, PDO::__construct() will always throw a PDOException exception. Uncaught exceptions are fatal.

Example #2 Create a PDO instance and set the error mode in the constructor

<?php
    $dsn = &#39;mysql:dbname=test;host=127.0.0.1&#39;;
    $user = &#39;googleguy&#39;;
    $password = &#39;googleguy&#39;;
    /*
     *使用 try/catch 围绕构造函数仍然有效,即使设置了 ERRMODE 为 WARNING,
     *因为如果连接失败,PDO::__construct 将总是抛出一个 PDOException 异常。
     */
    try {
        $dbh = new PDO($dsn, $user, $password, 
            array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
    } catch (PDOException $e) {
        echo &#39;Connection failed: &#39; . $e->getMessage();
        exit;
    }
// 这里将导致 PDO 抛出一个 E_WARNING 级别的错误,而不是 一个异常 (当数据表不存在时)
$dbh->query("SELECT wrongcolumn FROM wrongtable");
?>
Copy after login

The above routine will output:

Warning: PDO::query(): SQLSTATE[42S02]: Base table or view not found: 1146 Table &#39;test.wrongtable&#39; doesn&#39;t exist in/tmp/pdo_test.php on line 18
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template