← Back to PHP Course | Chapter 11: Database | Lesson 8 of 21

PHP Error Handling in DB

Database error handling चीज़ें गलत होने पर तैयार रहने के बारे में है, अगर school bus टूट जाए तो एक plan होने जैसा। PHP problem catch करता है और crash होने के बजाय politely respond करता है।
Syntax
php
try {
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->query("SQL statement");
} catch (PDOException $e) {
    error_log($e->getMessage());   // log details, show a friendly message
}

Database Exceptions Catch करना

Database operations कई कारणों से fail हो सकती हैं — एक lost connection, एक constraint violation, किसी query में एक syntax error — और आपका code उन failures पर कैसे react करता है यह decide करता है कि user को एक clear message दिखे या एक broken page।

उदाहरण: Catching Database Exceptions

php
<?php
// Try running this block; jump to `catch` if it throws
try {
    // Create a new `PDO` instance with 'sqlite::memory:', stored in `$pdo`
    $pdo = new PDO('sqlite::memory:');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->exec("SELECT * FROM missing_table");
// Catch PDOException $e
} catch (PDOException $e) {
    // Print "A clear error was caught instead of a broken page" to the output
    echo "A clear error was caught instead of a broken page";
}
?>

Error Codes पढ़ना

PDO::ATTR_ERRMODE को PDO::ERRMODE_EXCEPTION पर set करना (या हर mysqli call के बाद mysqli_error() check करना) ensure करता है कि database failures silently ignore होकर आगे confusing bugs का कारण बनने के बजाय तुरंत surface हों।

उदाहरण: Reading Error Codes

php
<?php
// Create a new `PDO` instance with 'sqlite::memory:', stored in `$pdo`
$pdo = new PDO('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Print "Errors will now surface immediately as exceptions" to the output
echo "Errors will now surface immediately as exceptions";
?>

Custom Error Loggers

database calls को एक try/catch block में wrap करना आपको specifically एक PDOException catch करने देता है, developers के लिए technical details log करने देता है, और end user को एक technical न होने वाला friendly error message दिखाने देता है।

उदाहरण: Custom Error Loggers

php
<?php
// Try running this block; jump to `catch` if it throws
try {
    // Create a new `PDO` instance with 'sqlite::memory:', stored in `$pdo`
    $pdo = new PDO('sqlite::memory:');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->exec("SELECT * FROM missing_table");
// Catch PDOException $e
} catch (PDOException $e) {
    // Call `error_log($e->getMessage())`
    error_log($e->getMessage());
    // Print "Something went wrong. Please try again." to the output
    echo "Something went wrong. Please try again.";
}
?>

MySQLi Connection Errors

कभी raw database error messages सीधे end users को न दिखाएँ — वे आपकी schema या query structure के बारे में details leak कर सकते हैं जिन्हें एक attacker इस्तेमाल कर सकता है, और आमतौर पर एक non-technical visitor के लिए meaningless होते हैं।

उदाहरण: MySQLi Connection Errors

php
<?php
try {
    $pdo = new PDO('sqlite::memory:');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->exec("SELECT * FROM missing_table");
} catch (PDOException $e) {
    // Never echo $e->getMessage() directly to the visitor -- it can leak schema details
    echo "An error occurred.";
}
?>

Sensitive Details Suppress करना

पूरा exception (query और parameters सहित, लेकिन कभी raw passwords नहीं) एक server-side log file में log करना आपको एक issue debug करने के लिए ज़रूरी detail देता है बिना public को कुछ भी sensitive expose किए।

उदाहरण: Suppressing Sensitive Details

php
<?php
// Try running this block; jump to `catch` if it throws
try {
    // Create a new `PDO` instance with 'sqlite::memory:', stored in `$pdo`
    $pdo = new PDO('sqlite::memory:');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->exec("SELECT * FROM missing_table");
// Catch PDOException $e
} catch (PDOException $e) {
    // Call `error_log("DB error: " . $e->getMessage())`
    error_log("DB error: " . $e->getMessage());
    // Print "Logged for developers, hidden from the visitor." to the output
    echo "Logged for developers, hidden from the visitor.";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. visitors को raw database error messages दिखाना, जो table names और credentials leak कर सकता है।
  2. जब PDO exceptions throw करने के लिए set हो तो PDOException catch न करना, ताकि script एक fatal error के साथ खत्म हो।
  3. error code को ignore करना, ताकि एक duplicate key जैसी अलग-अलग failures को same treat किया जाए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.