PDO Error Handling:
In this current tutorial we will study about the error handling methodology in PDO extension. In general we use try and catch block to handle the errors in the database connection but to handle other kind of errors and warnings could be handled as well in this extension. Following example illustrates this concept:
Example:
<?php
$host
='localhost';$user
='root';$pass
='';$db
='DB';try
{ $dbh=new PDO("mysql:$host;dbname=$db",$user,$pass); $sql = "SELECT username FROM emp_details"; $result=$dbh->exec($sql); foreach ($dbh->query($sql) as $row){
print $row['emp_first_name'] .' - '. $row['emp_last_name'] . '<br />';}
$dbh = null;}
catch
(PDOException $e){
echo $e->getMessage();}
?>
Output:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\PHP-AJAX\pdo.php
on line 10
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.