Reputation: 383
when I use PDO errorInfo
I get this :
Array (
[0] => 23000
[1] => 1062
[2] => Duplicate entry '[email protected]' for key 'email'
)
but I wanna get only column name > 'email' and 1062 for error code to echo :
this email [email protected] already registered
because I use email or phone or username for registration
Upvotes: 2
Views: 265
Reputation: 6766
I think the only way of doing it is to use regular expression. You can use preg_replace_callback to search and replace the text.
list(, $code, $message) = $dbh->errorInfo();
// check if duplicate error
if ($code === 1062) {
echo preg_replace_callback("/^Duplicate entry '(.*)' for key '(.*)'$/", function ($m) {
return sprintf("this %s %s already registered", $m[2], $m[1]);
}, $message);
// this email [email protected] already registered
}
Upvotes: 1