Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SQLSTATE: 23000 (ER_DUP_KEY)
- 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());
- }
- }
- 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());
- }
- }
- 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();
- }
- }
- SET lc_messages = 'es_ES'
- 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'; SET lc_messages = 'es_ES'");
- $this->pdo->exec("SET NAMES 'utf8'; SET lc_messages = 'es_ES'");
- $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- } catch (PDOException $e) {
- die($e->getMessage());
- }
- }
- class DB
- {
- private $pdo;
- 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();
- }
- }
- class MyPDOException extends PDOException {
- function __construct($message = null, $code = 0, Exception $previous = null) {
- switch ($code) {
- case 23000:
- $message = 'Violación de restricciones de integridad';
- break;
- default:
- break;
- }
- parent::__construct($message, $code, $previous);
- }
- }
- class MyPDO extends PDO {
- public function __construct (string $dsn, string $username = null, string $password = null, array $options = array()) {
- try {
- return parent::__construct($dsn, $username, $password, $options);
- } catch (PDOException $e) {
- throw new MyPDOException($e->getMessage(), $e->getCode(), $e);
- }
- }
- public function exec (string $statement) {
- try {
- return parent::exec($statement);
- } catch (PDOException $e) {
- throw new MyPDOException($e->getMessage(), $e->getCode(), $e);
- }
- }
- // etc.
- }
- catch(Exception $e){
- switch ($e->getCode()) {
- case 23000:
- $serror= $e->getMessage();
- $eerror= explode(" ", $serror);
- if (in_array("1451",$eerror))
- {
- //Lo que quieres que haga para el el error 1451 que esta dentro del 23000
- }
- else
- {
- // Por si no tienes el error listado muestre el general
- }
- default:
- return $e->getMessage();
- }
- }
Add Comment
Please, Sign In to add comment