Skip to content

Instantly share code, notes, and snippets.

@alexsegura
Created March 31, 2014 15:52
Show Gist options
  • Save alexsegura/9895452 to your computer and use it in GitHub Desktop.
Save alexsegura/9895452 to your computer and use it in GitHub Desktop.
Get MySQL error code with Doctrine DBAL
<?php
$sql = '...';
$stmt = $conn->prepare($sql);
try {
$stmt->execute();
} catch (\Doctrine\DBAL\DBALException $e) {
$previous = $e->getPrevious();
if ($previous instanceof \Doctrine\DBAL\Driver\Mysqli\MysqliException) {
// $errorCode contains MySQL error code (ex: 1062 for a duplicate entry)
$errorCode = $previous->getCode();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment