I am performing a CRUD, and I am giving the final blows for its completion, because the issue is, for example, if we put a duplicate PK in one of the tables, it throws this error due to the PDOException:
SQLSTATE: 23000 (ER_DUP_KEY)
But of course, I would like to know if there was any way to modify that message. When I see that, I know it's a duplicate primary key, but for example, a user who has never seen anything in his life from DB may not, I don't know if I'm explaining myself, I know that if I put an echo
over it, it will display it, but of course, it doesn't always work to be that the error there may be others.
Thanks for your help.
This would be the construction towards the BBDD
public function __CONSTRUCT() {
try {
$this->pdo = new PDO('mysql:host=localhost;dbname=prueba;charset=UTF8', 'root', '');
$this->pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
$this->pdo->exec("SET NAMES 'utf8';");
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
die($e->getMessage());
}
}
And this for example the insert function:
public function Registrar(Categoria $data) {
try {
$sql = "INSERT INTO categoria (acronimo,categoria,registro_calidad)
VALUES (?, ?,?)";
//Ejecucion de la consulta siguiente
$this->pdo->prepare($sql)->execute(array(
$data->__GET('acronimo'),
$data->__GET('categoria'),
$data->__GET('registro_calidad')
)
);
} catch (PDOException $e) {
die($e->getMessage());
}
}
PS Edition of the code for a last query made in the response that was assigned as valid.
public function __construct() {
try {
$this->pdo = new PDO('mysql:host=localhost;dbname=prueba;charset=UTF8', 'root', '');
$this->pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
$this->pdo->exec("SET NAMES 'utf8';");
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
die($this->getErrorMsg($e));
}
}
protected function getErrorMsg(PDOException $e) {
switch ($e->getCode()) {
case 23000:
//AQUI Analizar cual de todos los tipos es
return 'Violación de restricciones de integridad';
default:
return $e->getMessage();
case 25000:
//AQUI Analizar cual de todos los tipos es
return 'Violación de restricciones de integridad';
default:
return $e->getMessage();
}
}
Translated error messages
From
PHP
to indicateMySQL
the language of the error messages you can uselc_messages
Example:
Using your code, you could do the following:
Custom error messages
Option 1: Mapping
PDOException
provides the methodgetCode
, which returns only thecódigo de la excepción
.If you also have an array where to map the errors by their code, you can return a custom message.
Example:
Option 2: OOP
Create your own extended class of
PDOException
, to customize the messages.Create your own extended class of , and override the , , , etc.
PDO
methods , catch the exceptions and throw custom ones instead. Use this new class to connect to the .__construct
exec
query
DB
Example:
I don't know if it's too late but, I solved that problem when doing the catch in the following way:
I explain: Just in case getCode() returns error 23000, I store the error string in a variable, then separate that string into an array every time there is a space with explode(), and then with the in_array() function of php I look if the code 1451 is in the string.
I don't know if this is the best way, but it has worked for me. Successes. She left the code:
After the try {}