Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- CONFIGURAÇÃO DO SISTEMA ( CAMILLE )
- INOVE WS
- (BANCO DE DADOS)
- @programador Paulo Ricardo Santos Godoi. (http://facebook.com/ricardo.sis)
- @versao 0.1a
- Sistema baseado no script (https://github.com/matyre73)
- @author: Wade Dunbar
- @version: 1.0
- */
- // Precisa dessa Função para gerar logs de erros no script abaixo.
- require("Logs.php");
- class db_conectar{
- // Gera logs de erros.
- //private $log;
- // Definindo Variáveis de banco de dados
- //private $servidor, $usuario, $senha, $banco, $conexao, $charSet;
- private $hostname, $username, $password, $database, $dbDriver, $charSet;
- // Variáveis de Ligação
- private $BancoDeDados, $query_num, $insert_id;
- function __construct($settings){
- isset($settings['hostname']) ? $this->hostname = $settings['hostname']: die ("Servidor não encontrado!");
- isset($settings['username']) ? $this->username = $settings['username']: die ("Login não encontrado!");
- isset($settings['password']) ? $this->password = $settings['password']: die ("Senha incorreta!");
- isset($settings['database']) ? $this->database = $settings['database']: die ("Banco de Dados não encontrado!");
- $this->dbDriver = isset($settings['dbDriver']) ? $settings['dbDriver']: "mysql";
- $this->charSet = isset($settings['charSet']) ? $settings['charSet']: "utf8";
- // Conecta-se ao Banco de Dados.
- $this->log = new Log();
- $this->connect();
- }
- // Função que conecta-se no banco de dados
- // É necessário ter definido as seguintes constantes: DB_NAME, DB_HOST, DB_USER, DB_PASSWORD
- private function connect(){
- try{
- $this->db_conectar = new PDO("$this->dbDriver:host=$this->hostname;dbname=$this->database;$this->charSet", $this->username, $this->password);
- $this->error_handling();
- } catch (PDOException $error) {
- // Date para registro nos logs
- $date = date("d/m : H:i : ");
- //Criar log's de erro de conexão
- file_put_contents('../includes/logs/db.conexao.error.txt', $date.$error->getMessage().PHP_EOL,FILE_APPEND);
- echo 'Atenção! Ocorreu um erro ao tentar se conectar ao banco de dados. <br/> Caso o erro persistir contate o administrador do sistema! <br/> ', $e->getMessage();
- // Deixar Página em branco ao exibir os erros.
- die($error);
- }
- return $this->db_conectar;
- }
- /*private function connect(){
- try{
- $this->db_conectar = new PDO("$this->dbDriver:host=$this->hostname;dbname=$this->database;$this->charSet", $this->username, $this->password);
- $this->error_handling();
- }catch(PDOException $error){
- die($error);
- }
- }
- */
- private function error_handling(){
- if($this->db_conectar){
- $this->db_conectar->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->db_conectar->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- }
- }
- private function _print($var){
- echo is_array($var) ? "<pre>".print_r($var,true)."</pre>": $var."<br>";
- }
- // Função que faz a consulta da tabela no banco de dados.
- /*
- // Insert
- $consulta = $BancoDeDados->query("SELECT * FROM Table");
- */
- function query($query, $show = null){
- ($show) ? $this->_print($query): "";
- if($consulta = $this->db_conectar->query($query)):
- return $consulta;
- else:
- $this->_print($this->db_conectar->errorInfo());
- endif;
- }
- // Função que faz a consulta da tabela,coluna no banco de dados.
- /*
- // Insert
- $consulta = $BancoDeDados->query_prepare("SELECT * FROM someTable WHERE something = :comparison", array(':comparison' => $comparison));
- $data = $BancoDeDados->fetch_array($consulta);
- */
- function query_prepare($query, $prepare, $show = false){
- if($show){
- $this->_print($query);
- $this->_print($prepare);
- }
- $consulta = $this->db_conectar->prepare($query);
- if($consulta->execute($prepare)){
- return $consulta;
- }else{
- $this->_print($this->db_conectar->errorInfo());
- }
- }
- function num_rows_affected(&$consulta){
- return (isset($consulta))? $consulta->rowCount(): "";
- }
- function fetch_array(&$consulta){
- $fetchType = PDO::FETCH_ASSOC;
- return (isset($consulta)) ? $consulta->fetchAll($fetchType): "";
- }
- function fetch_array_assoc(&$consulta){
- $fetchType = PDO::FETCH_ASSOC;
- return (isset($consulta)) ? $consulta->fetchAll($fetchType): "";
- }
- function fetch_both(&$consulta){
- $fetchType = PDO::FETCH_BOTH;
- return (isset($consulta)) ? $consulta->fetchAll($fetchType): "";
- }
- function fetch_bound(&$consulta){
- $fetchType = PDO::FETCH_BOUND;
- return (isset($consulta)) ? $consulta->fetchAll($fetchType): "";
- }
- function fetch_class(&$consulta){
- $fetchType = PDO::FETCH_CLASS;
- return (isset($consulta)) ? $consulta->fetchAll($fetchType): "";
- }
- function fetch_num(&$consulta){
- $fetchType = PDO::FETCH_NUM;
- return (isset($consulta)) ? $consulta->fetchAll($fetchType): "";
- }
- function fetch_object(&$consulta){
- $fetchType = PDO::FETCH_OBJ;
- return (isset($consulta)) ? $consulta->fetchAll($fetchType): "";
- }
- function num_rows($consulta){
- return (isset($consulta)) ? $consulta->rowCount(): "";
- }
- // Função que cadastra algo no banco de dados.
- /*
- // Insert
- $insert = $BancoDeDados->insert("nome_tabela", array("nome_coluna"=>"valor_da_coluna"));
- // Do something with the data
- if($insert > 0 ) {
- return 'Parabens! Dados inseridos com sucesso!';
- }
- */
- function insert($table, $arFieldValues, $show = FALSE){
- $escVals = array();$excVals = array();$cnt = 0;
- $fields = array_keys($arFieldValues);
- $values = array_values($arFieldValues);
- foreach($values as $val){
- $key = ":$cnt";
- $escVals[] = $key;
- $excVals[$key] = $val;
- $cnt++;
- }
- $sql = " INSERT INTO $table (" . join(', ',$fields) . ") VALUES(" . join(', ',$escVals) . ")";
- ($show) ? $this->_print($sql): "";
- $consulta = $this->db_conectar->prepare($sql);
- $res = $consulta->execute($excVals);
- $this->insert_id = $this->db_conectar->lastInsertId();
- return $res;
- }
- // Função que atualiza dados no banco de dados
- /*
- // Update
- $update = $BancoDeDados->update('nome_tabela',array('thisfield' => value, 'field2'=>value2),array('conditionfield'=>conValue));
- */
- function update($table, $arFieldValues, $arConditions, $show = FALSE){
- $arUpdates = array();$excVals = array();$arWhere = array();$cnt = 0;
- foreach($arFieldValues as $field => $val){
- $key = ":$cnt";
- $arUpdates[] = "$field = $key";
- $excVals[$key] = $val;
- $cnt++;
- }
- foreach($arConditions as $field => $val){
- $key = ":$cnt";
- $arWhere[] = "$field = $key";
- $excVals[$key] = $val;
- $cnt++;
- }
- $sql = "UPDATE $table SET ". join(', ',$arUpdates) . " WHERE " . join(' AND ',$arWhere);
- if($show){
- $this->_print($sql);
- $this->_print($excVals);
- }
- $consulta = $this->db_conectar->prepare($sql);
- $res = $consulta->execute($excVals);
- return $res;
- }
- // Função que deleta algo do banco de dados
- /*
- // Delete
- $delete = $BancoDeDados->delete("nome_tabela", array("nome_coluna"=>"valor_da_coluna"));
- */
- function delete($table, $arConditions, $show = FALSE){
- $arWhere = array();$excVals = array();$cnt = 0;
- foreach($arConditions as $field => $val){
- $key = ":$cnt";
- $arWhere[] = "$field = $key";
- $excVals[$key] = $val;
- $cnt++;
- }
- $sql = "DELETE FROM $table WHERE " . join(' AND ',$arWhere);
- ($show) ? $this->_print($sql): "";
- $consulta = $this->db_conectar->prepare($sql);
- foreach($excVals as $k => $v){
- $consulta->bindParam("$k", $v);
- }
- $res = $consulta->execute();
- return $res;
- }
- function last_insert_id($var = FALSE){
- $id = ($var) ? $this->db_conectar->lastInsertId(): ($this->db_conectar) ? $this->insert_id: 0;
- return $id;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement