PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP errorCode() And errorInfo(): Error Handling In PDO

The errorCode() and errorInfo() methods are part of the PDO (PHP Data Objects) class in PHP, which is used to interact with databases. These methods are used to get information about errors that occur when interacting with a database.

errorCode()

The errorCode() method is used to fetch the SQLSTATE associated with the last operation on the database handle.

The SQLSTATE is a standard set of error codes for SQL. It's a five characters long alphanumeric identifier defined in the ANSI SQL standard. The first two characters define the class of the error (e.g., "42" is for syntax errors) and the last three define the subclass (e.g., "000" means "no further subclass").

Here is an example of its usage:

<?php
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "");
$stmt = $pdo->prepare('SELECT * FROM non_existent_table');
if (!$stmt->execute()) {
    echo $pdo->errorCode();
}

In this example, we're trying to select data from a table that does not exist. The execute method will return false and errorCode will print an error code.

errorInfo()

The errorInfo() method returns an array of error information about the last operation performed by this database handle. The array consists of the following fields:

  • 0: SQLSTATE error code (a five characters alphanumeric identifier for the error)
  • 1: Driver-specific error code
  • 2: Driver-specific error message

Here is an example of its usage:

<?php
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "");
$stmt = $pdo->prepare('SELECT * FROM non_existent_table');
if (!$stmt->execute()) {
    $errorInfo = $pdo->errorInfo();
    print_r($errorInfo);
}

In this example, we're trying to select data from a table that does not exist. The execute method will return false and errorInfo will print an array with the SQLSTATE error code, MySQL error code, and MySQL error message.

Remember, these methods will only provide useful information if the last operation on the database handle resulted in an error. If the last operation was successful, these methods may not return useful information.

  1. Handling errors and exceptions in PHP PDO:

    • Use try-catch blocks to handle exceptions during PDO operations.
    try {
        // PDO code here
    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
    
  2. Retrieving error codes with errorCode() in PDO:

    • errorCode() returns the SQLSTATE error code.
    $stmt = $pdo->prepare("SELECT * FROM non_existent_table");
    $stmt->execute();
    
    $errorCode = $stmt->errorCode();
    echo "Error code: $errorCode";
    
  3. Getting detailed error information with errorInfo() in PDO:

    • errorInfo() returns an array with error information.
    $stmt = $pdo->prepare("SELECT * FROM non_existent_table");
    $stmt->execute();
    
    $errorInfo = $stmt->errorInfo();
    echo "Error info: " . implode(", ", $errorInfo);
    
  4. Interpreting PDO error codes and messages in PHP:

    • SQLSTATE codes provide information about the type of error.
    $stmt = $pdo->prepare("SELECT * FROM non_existent_table");
    $stmt->execute();
    
    $errorInfo = $stmt->errorInfo();
    echo "SQLSTATE: {$errorInfo[0]}, Code: {$errorInfo[1]}, Message: {$errorInfo[2]}";
    
  5. Custom error handling strategies with PDO in PHP:

    • Implement custom error handling using set_exception_handler().
    function customExceptionHandler($exception) {
        echo "Custom Exception Handler: " . $exception->getMessage();
    }
    
    set_exception_handler('customExceptionHandler');
    
    try {
        // PDO code here
    } catch (PDOException $e) {
        // Exception handled by custom handler
    }
    
  6. Logging and recording PDO errors with errorCode() and errorInfo():

    • Log error information for debugging or record-keeping.
    $stmt = $pdo->prepare("SELECT * FROM non_existent_table");
    $stmt->execute();
    
    $errorCode = $stmt->errorCode();
    $errorInfo = $stmt->errorInfo();
    
    // Log error information
    error_log("SQLSTATE: $errorCode, Code: {$errorInfo[1]}, Message: {$errorInfo[2]}");
    
  7. Handling transaction errors using PDO error functions:

    • Handle errors specific to transactions.
    try {
        $pdo->beginTransaction();
    
        // SQL queries here
    
        $pdo->commit();
    } catch (PDOException $e) {
        $pdo->rollBack();
        echo "Transaction failed: " . $e->getMessage();
    }
    
  8. Displaying user-friendly error messages in PDO:

    • Present user-friendly messages while logging detailed information.
    try {
        // PDO code here
    } catch (PDOException $e) {
        echo "An error occurred. Please try again later.";
        error_log("PDO Error: " . $e->getMessage());
    }