Guest User

Untitled

a guest
Nov 26th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. <?php
  2. ini_set('display_errors', '1');
  3. error_reporting(E_ALL & ~E_NOTICE);
  4. date_default_timezone_set('america/bogota');
  5. setlocale(LC_ALL, "es_ES@euro", "es_CO", "esp");
  6. class server
  7. {
  8.  
  9. private static $db_host = 'localhost';
  10. private static $db_user = '';
  11. private static $db_pass = '';
  12. private $db_name = '';
  13. private $conexion;
  14. private $resultado;
  15.  
  16. public function __construct()
  17. {
  18. $this->conectar();
  19. }
  20.  
  21. protected function conectar()
  22. {
  23.  
  24. $this->conexion = mysqli_connect(
  25. self::$db_host,
  26. self::$db_user,
  27. self::$db_pass,
  28. $this->db_name);
  29.  
  30. if (!$this->conexion) {
  31. mysqli_error($this->conexion);
  32. } else {
  33. return true;
  34. }
  35. }
  36.  
  37. protected function query($sql)
  38. {
  39. $this->resultado = mysqli_query($this->conexion, $sql);
  40. if (!$this->resultado) {
  41. die('error: query ' . mysql_error());
  42. }
  43.  
  44. }
  45.  
  46. protected function extraerRegistro()
  47. {
  48. $fila = mysqli_fetch_assoc($this->resultado);
  49. return $fila;
  50. }
  51.  
  52. public function affected()
  53. {
  54.  
  55. $num = mysqli_affected_rows($this->conexion);
  56. if ($num) {
  57. return true;
  58. } else {
  59. return false;
  60. }
  61.  
  62. }
  63.  
  64. }
  65.  
  66. // se asume conexión en $mysqli
  67.  
  68. // creamos una bandera
  69. $result_transaccion = true;
  70.  
  71. // iniciamos transacción
  72. $mysqli->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  73.  
  74. // realizamos las querys
  75. if( !$mysqli->query("INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)") ) {
  76. // registramos el fallo
  77. $result_transaccion = false;
  78. }
  79.  
  80. if ( !$mysqli->query("INSERT INTO Language VALUES ('DEU', 'Swabian', 'F', 9.4)") ) {
  81. // registramos el fallo
  82. $result_transaccion = false;
  83. }
  84.  
  85. if($result_transaccion) {
  86. // consignamos
  87. $mysqli->commit();
  88. } else {
  89. // revertimos
  90. $mysqli->rollback();
  91. }
  92.  
  93. $mysqli->close();
Add Comment
Please, Sign In to add comment