Guest User

Untitled

a guest
Jan 22nd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. SQLSTATE: 23000 (ER_DUP_KEY)
  2.  
  3. public function __CONSTRUCT() {
  4. try {
  5. $this->pdo = new PDO('mysql:host=localhost;dbname=prueba;charset=UTF8', 'root', '');
  6. $this->pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
  7. $this->pdo->exec("SET NAMES 'utf8';");
  8. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  9. $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  10. } catch (PDOException $e) {
  11. die($e->getMessage());
  12. }
  13. }
  14.  
  15. public function Registrar(Categoria $data) {
  16. try {
  17. $sql = "INSERT INTO categoria (acronimo,categoria,registro_calidad)
  18. VALUES (?, ?,?)";
  19. //Ejecucion de la consulta siguiente
  20. $this->pdo->prepare($sql)->execute(array(
  21. $data->__GET('acronimo'),
  22. $data->__GET('categoria'),
  23. $data->__GET('registro_calidad')
  24. )
  25. );
  26. } catch (PDOException $e) {
  27. die($e->getMessage());
  28. }
  29. }
  30.  
  31. public function __construct() {
  32. try {
  33. $this->pdo = new PDO('mysql:host=localhost;dbname=prueba;charset=UTF8', 'root', '');
  34. $this->pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
  35. $this->pdo->exec("SET NAMES 'utf8';");
  36. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  37. $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  38. } catch (PDOException $e) {
  39. die($this->getErrorMsg($e));
  40. }
  41. }
  42.  
  43. protected function getErrorMsg(PDOException $e) {
  44. switch ($e->getCode()) {
  45. case 23000:
  46. //AQUI Analizar cual de todos los tipos es
  47. return 'Violación de restricciones de integridad';
  48. default:
  49. return $e->getMessage();
  50. case 25000:
  51. //AQUI Analizar cual de todos los tipos es
  52. return 'Violación de restricciones de integridad';
  53. default:
  54. return $e->getMessage();
  55. }
  56. }
  57.  
  58. SET lc_messages = 'es_ES'
  59.  
  60. public function __CONSTRUCT() {
  61. try {
  62. $this->pdo = new PDO('mysql:host=localhost;dbname=prueba;charset=UTF8', 'root', '');
  63. $this->pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'; SET lc_messages = 'es_ES'");
  64. $this->pdo->exec("SET NAMES 'utf8'; SET lc_messages = 'es_ES'");
  65. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  66. $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  67. } catch (PDOException $e) {
  68. die($e->getMessage());
  69. }
  70. }
  71.  
  72. class DB
  73. {
  74. private $pdo;
  75.  
  76. public function __construct()
  77. {
  78. try {
  79. $this->pdo = new PDO('mysql:host=localhost;dbname=prueba;charset=UTF8', 'root', '');
  80. $this->pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
  81. $this->pdo->exec("SET NAMES 'utf8';");
  82. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  83. $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  84. } catch (PDOException $e) {
  85. die($this->getErrorMsg($e));
  86. }
  87. }
  88.  
  89. protected function getErrorMsg(PDOException $e) {
  90. switch ($e->getCode()) {
  91. case 23000:
  92. // AQUI Analizar cual de todos los tipos es
  93. return 'Violación de restricciones de integridad';
  94. default:
  95. return $e->getMessage();
  96. }
  97. }
  98.  
  99. class MyPDOException extends PDOException {
  100.  
  101. function __construct($message = null, $code = 0, Exception $previous = null) {
  102.  
  103. switch ($code) {
  104. case 23000:
  105. $message = 'Violación de restricciones de integridad';
  106. break;
  107.  
  108. default:
  109. break;
  110. }
  111.  
  112. parent::__construct($message, $code, $previous);
  113. }
  114. }
  115.  
  116. class MyPDO extends PDO {
  117. public function __construct (string $dsn, string $username = null, string $password = null, array $options = array()) {
  118. try {
  119. return parent::__construct($dsn, $username, $password, $options);
  120. } catch (PDOException $e) {
  121. throw new MyPDOException($e->getMessage(), $e->getCode(), $e);
  122. }
  123. }
  124.  
  125. public function exec (string $statement) {
  126. try {
  127. return parent::exec($statement);
  128. } catch (PDOException $e) {
  129. throw new MyPDOException($e->getMessage(), $e->getCode(), $e);
  130. }
  131. }
  132.  
  133. // etc.
  134. }
  135.  
  136. catch(Exception $e){
  137. switch ($e->getCode()) {
  138. case 23000:
  139. $serror= $e->getMessage();
  140. $eerror= explode(" ", $serror);
  141.  
  142. if (in_array("1451",$eerror))
  143. {
  144. //Lo que quieres que haga para el el error 1451 que esta dentro del 23000
  145. }
  146. else
  147. {
  148. // Por si no tienes el error listado muestre el general
  149. }
  150. default:
  151. return $e->getMessage();
  152. }
  153.  
  154. }
Add Comment
Please, Sign In to add comment