L3 Info : PHP et Applications Web
 
◃  Ch. 5 PDO : PHP Data Object  ▹
 

Gestion d'erreurs

Code source

try {
    include 'dsn.php';

    $dbs = $dbh->query('SELECT aut_ville from auteur WHERE aut_nom = :auteur');
    if ($dbs) {
      $tab = $dbs->fetchAll($fetch_style = PDO::FETCH_ASSOC );
      afficheTable($tab);
    }
    else {
      echo "<p><b>". $dbh->errorCode() . "</b> : ";
      foreach($dbh->errorInfo() as $cle => $val)
	echo $cle."=".$val." ";
      echo "</p>";
    }

  } catch (PDOException $e) {
    print "<br />Erreur !: " . $e->getMessage() . "<br/>";
    die();
    }

Résultat

42000 : 0=42000 1=1064 2=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':auteur' at line 1