Advertisement
Guest User

Untitled

a guest
Jun 8th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.97 KB | None | 0 0
  1.     <?php
  2.     class Bd_Conexao
  3.     {
  4.         //para alterar estes valores vá ao arquivo configDB
  5.        private static $instancia = NULL; //se der prob mudar p private
  6.        private $server = SERVER;
  7.        private $user = USER;
  8.        private $password =PASSWORD ;
  9.        public static $conexao; //retirar static
  10.        public static $status ="Zerada!"; //retirar static
  11.  
  12.        protected function __construct()
  13.        {
  14.             if(self::$conexao==NULL){
  15.  
  16.                self::$conexao = mysqli_connect($this->server, $this->user, $this->password) or die("erro: ".mysqli_error(self::$conexao)." num: ".mysqli_errno(self::$conexao));
  17.                self::$status = "Iniciada!";
  18.            }
  19.            //$this->ver();
  20.           //return self::$conexao;
  21.  
  22.        }
  23.  
  24.        function  __toString() {
  25.            return self::$status;
  26.        }
  27.        //singleton da conexao
  28.  
  29.        public function getConexao()
  30.        {   //self::getInstancia();
  31.            if((self::$conexao==null) ||(self::$conexao==false)){
  32.  
  33.             //self::$conexao = mysqli_connect($this->server, $this->user, $this->password) or die("erro: ".mysqli_error(self::$conexao)." num: ".mysqli_errno(self::$conexao));
  34.             echo "Conexao inexistente!![] <br>";
  35.  
  36.            }else{echo "Conexao ok!![ ] <br>";}
  37.            //$this->ver();
  38.           return self::$conexao;
  39.        }
  40.  
  41.        //singleton da classe my_deprecated
  42.        public static function getInstancia()
  43.        {
  44.            if(self::$instancia==NULL)       {
  45.              self::$instancia = new self;
  46.  
  47.              echo "Nova instancia!![] <br>";
  48.            }else{
  49.                echo "instancia ja existente!![] <br>";
  50.            }
  51.            //self::ver();
  52.            return self::$instancia;
  53.        }
  54.  
  55.        public function query($sql)
  56.        {
  57.            self::$status = "Requerida Query!";
  58.            //echo var_dump(self::$conexao);
  59.            //return self::$conexao->query($sql)  or die("erro: ".mysqli_error(self::$conexao)." num: ".mysqli_errno(self::$conexao));
  60.            //mysqli_set_charset(self::$conexao,'utf8');
  61.            $result = mysqli_query(self::$conexao,$sql) or die("erro: ".mysqli_error(self::$conexao)." num: ".mysqli_errno(self::$conexao));
  62.            return $result;
  63.         }
  64.  
  65.        public function prepare($sql)
  66.        {  
  67.            $result = mysqli_prepare(self::$conexao,$sql) or die("erro: ".mysqli_error(self::$conexao)." num: ".mysqli_errno(self::$conexao));
  68.            return $result;
  69.        }
  70.  
  71.        public function multiquery($sql)
  72.        {
  73.            mysqli_set_charset($this->getConexao(),'utf8');
  74.            $r = mysqli_multi_query( $this->getConexao(),$sql) or die("erro: ".mysqli_error(self::$conexao)." num: ".mysqli_errno(self::$conexao));
  75.            return $r;
  76.         }
  77.  
  78.        public function select_db($db)
  79.        {   //$this->getConexao();
  80.            self::$status = "Requerira BD!";
  81.            $r = mysqli_select_db(self::$conexao, $db) or die("erro: ".mysqli_error(self::$conexao)." num: ".mysqli_errno(self::$conexao));
  82.            return  $r;
  83.         }
  84.  
  85.       //TRANSACTIONS
  86.        public function transaction()
  87.        {  
  88.            $r = mysqli_autocommit(self::$conexao, FALSE) or die("erro: ".mysqli_error(self::$conexao)." num: ".mysqli_errno(self::$conexao));
  89.            return  $r;
  90.         }
  91.  
  92.         public function commit()
  93.        {
  94.            $r = mysqli_commit(self::$conexao) or die("erro: ".mysqli_error(self::$conexao)." num: ".mysqli_errno(self::$conexao));
  95.            return  $r;
  96.         }
  97.  
  98.         public function rollback()
  99.        {
  100.            $r = mysqli_rollback(self::$conexao) or die("erro: ".mysqli_error(self::$conexao)." num: ".mysqli_errno(self::$conexao));
  101.            return  $r;
  102.         }
  103.  
  104.         public function __destruct()
  105.        {
  106.            if(self::$conexao){mysqli_close(self::$conexao) or die("Houve um erro ao fechar a conexão"); }
  107.            self::$conexao = "Finalizada!";
  108.            return $r;
  109.        }
  110.     }
  111.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement