Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1.  
  2. class Conexion{
  3.  
  4. protected $_lastid;
  5. protected $host;
  6. protected $username;
  7. protected $password;
  8. protected $dbname;
  9.  
  10. function Conexion(){
  11. $config = Zend_Registry::getInstance();
  12. $this->host = ['host'];
  13. $this->username = ['username'];
  14. $this->password = ['password'];
  15. $this->dbname = ['dbname'];
  16. }
  17.  
  18. public function getID()
  19. {
  20. return $this->_lastid;
  21. }
  22.  
  23. function transaccion($query,$encodeJSON=false,$encode=false){
  24.  
  25. $conexion = @mysql_connect($this->host, $this->username, $this->password);
  26.  
  27. @mysql_select_db($this->dbname, $conexion);
  28.  
  29. if($encode)
  30. {
  31. $executeQueryUTF8Format = @mysql_query('SET NAMES utf8;');
  32. }
  33.  
  34. if( $conexion == false ){ return false; }
  35.  
  36. /* Begin transaction. */
  37. $begin = @mysql_query("BEGIN;");
  38.  
  39. /* Execute operations that are part of the transaction. Commit on
  40. success, roll back on failure. */
  41. $executeQuery = @mysql_query($query);
  42.  
  43. $this->_lastid= @mysql_insert_id();
  44.  
  45. //If commit fails, roll back the transaction.
  46. $commit = @mysql_query("COMMIT;");
  47.  
  48. if ($begin && $executeQuery && $commit){
  49.  
  50. $data = array();
  51.  
  52. while ($row=@mysql_fetch_object($executeQuery)){
  53. $data [] = $row;
  54. }
  55.  
  56. // Liberar resultados
  57. @mysql_free_result($begin);
  58. @mysql_free_result($executeQuery);
  59. @mysql_free_result($commit);
  60.  
  61. // Cerrar la conexi�n
  62. @mysql_close($conexion);
  63.  
  64. if($encodeJSON)
  65. return json_encode($data);
  66. else
  67. return $data;
  68.  
  69. }else{
  70. @mysql_query("ROLLBACK");
  71.  
  72. // Liberar resultados
  73. @mysql_free_result($begin);
  74. @mysql_free_result($executeQuery);
  75. @mysql_free_result($commit);
  76.  
  77. // Cerrar la conexión
  78. @mysql_close($conexion);
  79.  
  80. return false;
  81. }
  82. }
  83.  
  84. }
  85.  
  86. se usa asi $this->conexion->transaccion("consulta",false,true);//devuelve un array si pone true en el segundo parametro devuelve un json
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement