Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.65 KB | None | 0 0
  1. <?php
  2.  
  3. class MySQL{
  4.     private $host;
  5.     private $user;
  6.     private $pass;
  7.     private $dbName;
  8.  
  9.     public function __construct($host, $user, $pass, $dbName){
  10.         $this->host = $host;
  11.         $this->user = $user;
  12.         $this->pass = $pass;
  13.         $this->dbName = $dbName;
  14.     }
  15.  
  16.     public function connect(){
  17.         if(!mysql_connect($this->host, $this->user, $this->pass)){
  18.             die('Erro de Conexão :'.mysql_error());
  19.         }else{
  20.             if(!mysql_select_db($this->dbName)){
  21.                 die('Erro de DB :'.mysql_error());
  22.             }
  23.         }
  24.     }
  25.  
  26.     function insert($table, array $data){
  27.         $value = array();
  28.         foreach($data as $v){
  29.             $value[] = "'".$this->escape($v)."'";
  30.         }
  31.         $strSQL = "INSERT INTO %s (%s) VALUES (%s) ";
  32.         $strSQL = sprintf($strSQL, $table, implode(',',array_keys($data)), implode(',', $value));
  33.         return $this->query($strSQL);
  34.     }
  35.  
  36.     function update($table, array $data, $where){
  37.         $strSQL = "UPDATE %s SET %s WHERE %s ";
  38.         $value  = array();
  39.         foreach ($data as $k => $v){
  40.             $value[] = sprintf("%s = '%s'", $k, $this->escape($v));
  41.         }
  42.         $strSQL = sprintf($strSQL, $table, implode(',', $value, $where));
  43.         return $this->query($strSQL);
  44.     }
  45.     function delete($table, $where){
  46.         $strSQL = "DELETE FROM %s WHERE %s";
  47.         $strSQL = sprintf($strSQL, $table, $where);
  48.         return $this->query($strSQL);
  49.     }
  50.  
  51.     function numRows($strSQL){
  52.         $query = $this->query($strSQL);
  53.         return mysql_num_rows($query);
  54.     }
  55.    
  56.     function fetchAll($strSQL){
  57.         $result = array();
  58.         $query  = $this->query($strSQL);
  59.         while($row = mysql_fetch_assoc($query)){
  60.             $result[] = $row;
  61.         }
  62.         return $result;
  63.     }
  64.     function fetchOne($strSQL){
  65.         $query  = $this->query($strSQL);
  66.         return mysql_fetch_assoc($query);
  67.     }
  68.  
  69.  
  70.     function query($strSQL){
  71.         if(!$conn = mysql_query($strSQL)){
  72.             die('Error '.mysql_error());
  73.         }
  74.         return $conn;
  75.     }
  76.  
  77.     function escape($str){
  78.         return mysql_real_escape_string($str);
  79.     }
  80.     function lastId(){
  81.         return mysql_insert_id();
  82.     }
  83.  
  84. }
  85.  
  86. $bd = new MySQL('localhost', 'root', '', 'banco_de_dados');
  87. $bd->connect();
  88.  
  89. //PARA INSERIR
  90. $data = array('campo1' => 'valor1', 'campo2' => 'valor2');
  91. $bd->insert('nome_da_tabela', $data);
  92.  
  93. //PARA ALTERAR
  94. $data = array('campo1' => 'valor11', 'campo2' => 'valor22');
  95. $bd->update('nome_de_tabela', $data, "idTabela = 1");
  96.  
  97. //PARA EXCLUIR
  98. $bd->delete('nome_de_tabela', "idTabela = 1");
  99.  
  100. ///Lista todo os itens
  101. $strSQL = "SELECT * FROM tabela";
  102. foreach ($this->fetchAll($strSQL) as $k => $v){
  103.         echo $v['nome_do_campo'];
  104. }
  105.  
  106. //Retornar apenas uma linha
  107. $strSQL = "SELECT * FROM tabela WHERE idTabela = 1";
  108. $row  = $bd->fetchOne($strSQL);
  109.  
  110. //Total de Registro
  111.  
  112. $strSQL = "SELECT * FROM tabela";
  113. echo $db->numRows($strSQL);
  114.  
  115.  
  116.  
  117.  
  118. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement