PHP Error Handling in DB
In this page:
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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.";
}
?>
Login to try C/C++/Java/PHP code in the editor
MySQLi Connection Errors
कभी raw database error messages सीधे end users को न दिखाएँ — वे आपकी schema या query structure के बारे में details leak कर सकते हैं जिन्हें एक attacker इस्तेमाल कर सकता है, और आमतौर पर एक non-technical visitor के लिए meaningless होते हैं।
उदाहरण: MySQLi Connection Errors
<?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.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Sensitive Details Suppress करना
पूरा exception (query और parameters सहित, लेकिन कभी raw passwords नहीं) एक server-side log file में log करना आपको एक issue debug करने के लिए ज़रूरी detail देता है बिना public को कुछ भी sensitive expose किए।
उदाहरण: Suppressing Sensitive Details
<?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.";
}
?>
Login to try C/C++/Java/PHP code in the editor
- visitors को raw database error messages दिखाना, जो table names और credentials leak कर सकता है।
- जब PDO exceptions throw करने के लिए set हो तो
PDOExceptioncatch न करना, ताकि script एक fatal error के साथ खत्म हो। - error code को ignore करना, ताकि एक duplicate key जैसी अलग-अलग failures को same treat किया जाए।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- PHP MySQL Introduction
- PHP MySQLi Connection
- PHP PDO Introduction
- PHP CRUD Operations
- PHP Prepared Statements
- PHP Stored Procedures
- PHP Transactions
- PHP Error Handling in DB
- PHP MySQL Connect
- PHP MySQL Create DB
- PHP MySQL Create Table
- PHP MySQL Insert Data
- PHP MySQL Get Last ID
- PHP MySQL Insert Multiple
- PHP MySQL Prepared Statements
- PHP MySQL Select Data
- PHP MySQL Where
- PHP MySQL Order By
- PHP MySQL Delete Data
- PHP MySQL Update Data
- PHP MySQL Limit Data