Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. mysqli_report(MYSQLI_REPORT_ERROR / MYSQLI_REPORT_STRICT);
  2.  
  3. require "conexion_libreria.php";
  4.  
  5. class Libreria extends ConexionLibreria {
  6.  
  7.  
  8. public function consultarLibros() {
  9. try {
  10.  
  11. $sql ="SELECT * FROM libros ORDER BY titulo";
  12.  
  13. $resultado = $this->conexionLibros->query($sql);
  14.  
  15. $libros = $resultado->fetch_all(MYSQLI_ASSOC);
  16.  
  17. return $libros;
  18. } catch (Exception $e) {
  19. throw new Exception("Hubo un error en el select: ".$e->getMessage());
  20. }
  21. }
  22. public function altaLibros($titulo, $precio) {
  23. try {
  24. $sql="INSERT INTO libros VALUES(NULL, '$titulo', '$precio')";
  25.  
  26. $this->conexionLibros->query($sql);
  27.  
  28. return "Alta efectuada";
  29. } catch (Exception $e) {
  30. if ($e->getCode()=='1062') {
  31. throw new Exception("Libro ya existe en la base de datos");
  32. } else {
  33. throw new Exception("Error en el alta: ".$e->getCode().'/'.$e->getFile().'/'.$e->getLine().'/'.$e->getMessage());
  34.  
  35. }
  36. }
  37. }
  38.  
  39. public function bajaLibros($id) {
  40. try {
  41. $sql="DELETE FROM libros WHERE idlibros='$id'";
  42.  
  43. $this->conexionLibros->query($sql);
  44.  
  45. return "Baja efectuada";
  46. } catch (Exception $e) {
  47.  
  48. if ($e->getCode()=='1062') {
  49. throw new Exception("Libro no existe en la base de datos");
  50. } else {
  51. throw new Exception("Error en la baja: ".$e->getCode().'/'.$e->getFile().'/'.$e->getLine().'/'.$e->getMessage());
  52.  
  53. }
  54. }
  55. }
  56.  
  57. public function modificaLibros($id, $titulo, $precio) {
  58. try {
  59. $sql="UPDATE libros SET idlibros='$id', titulo='$titulo', precio='$precio' WHERE idlibros='$id'";
  60.  
  61. $this->conexionLibros->query($sql);
  62.  
  63. return "Modificación efectuada";
  64. } catch (Exception $e) {
  65.  
  66. if ($e->getCode()=='1062') {
  67. throw new Exception("Libro no existe en la base de datos");
  68. } else {
  69. throw new Exception("Error en la modificación: ".$e->getCode().'/'.$e->getFile().'/'.$e->getLine().'/'.$e->getMessage());
  70.  
  71. }
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement