Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.10 KB | None | 0 0
  1. <?php
  2.         class MySQL{
  3.             private static $conn;
  4.             private $host;
  5.             private $user;
  6.             private $pass;
  7.             private $bd;
  8.            
  9.             public function __construct($host, $user, $pass, $bd){
  10.                 $this->host = $host;
  11.                 $this->user = $user;
  12.                 $this->pass = $pass;
  13.                 $this->bd   = $bd;
  14.                 $this->connect();
  15.             }
  16.            
  17.             //Conexão com banco de dados
  18.             public function connect(){
  19.                 if(!$conn = mysql_connect($this->host, $this->user, $this->pass)){
  20.                     die('Erro ao conectar :'.mysql_error());
  21.                 }else{
  22.                     if(!mysql_select_db($this->db, $conn)){
  23.                     die('Erro ao selecionar :'.mysql_error());
  24.                     }
  25.                 }
  26.             }
  27.             //executa o SQL
  28.             public function query($sql){
  29.                     if(!$query = mysql_query($sql)){
  30.                         die('Erro executar query :'.mysql_error());
  31.                     }else{
  32.                         return $query;
  33.                     }
  34.                
  35.             }
  36.             //inseri dados
  37.             public function insert($table, array $data){
  38.                 $strSQL = "INSERT INTO %s (%s) VALUES(%s)";
  39.                 $value = array();
  40.                 foreach($data as $k => $v){
  41.                     $value[] = sprintf("'%s'", $this->escape($v));
  42.                 }
  43.                 $strSQL = sprintf($strSQL, $table, implode(', ', array_keys($data)), implode(', ', $value));
  44.                 $this->query($strSQL);         
  45.             }
  46.            
  47.             //altera dados
  48.             public function update($table, array $data, array $where){
  49.                 $strSQL = "UPDATE %s SET %s WHERE %s";
  50.                 $field  = array();
  51.                 $fieldW = array();
  52.                 foreach($data as $k => $v){
  53.                      $field[] = sprintf("%s='%s'", $k, $this->escape($v));
  54.                 }
  55.                 foreach($where as $k => $v){
  56.                     $fieldW[] =  sprintf("%s='%s'", $k, $this->escape($v));
  57.                 }
  58.                 $strSQL = sprintf($strSQL, $table, implode(', ',  $field), implode(' AND', $fieldW));
  59.                 $this->query($strSQL); 
  60.             }
  61.            
  62.             //excluir dados
  63.             public function delete($table, array $where){
  64.                 $strSQL = "DELETE FROM %s WHERE %s";
  65.                 $field  = array();
  66.                 foreach($where as $k => $v){
  67.                     $field[] =  sprintf("%s='%s'", $k, $this->escape($v));
  68.                 }
  69.                 $strSQL = sprintf($strSQL, $table, implode(' AND ',  $field));
  70.                 $this->query($strSQL); 
  71.             }
  72.            
  73.             //resume a execução dos comandos INSERT, UPDATE, DELETE
  74.            
  75.             public function execute($type, $table, $data=null, $where = null){
  76.                     swicth($type){
  77.                         case 'insert':
  78.                             $this->insert($table, $data);
  79.                         break;
  80.                         case 'update':
  81.                             $this->update($table, $data, $where);
  82.                         break;
  83.                        
  84.                         case 'delete':
  85.                             $this->update($table, $where);
  86.                         break;
  87.                     }
  88.             }
  89.            
  90.            
  91.             //escapa a string
  92.             public function escape($value){
  93.                 return mysql_real_escape_string(stripslashes($value));
  94.             }
  95.         }
  96.        
  97.         //********************************UTILIZANDO******************************************//
  98.        
  99.         $mysql = new MySQL('localhost', 'root', '', 'banco_de_dados');
  100.         //INSERIR DADOS
  101.         $data  = array('nome' => 'David', 'estado' =>'rj');
  102.         $mysql->execute('insert', 'contato', $data);
  103.         //ALTERAR DADOS
  104.         $data  = array('nome' => 'David CHC', 'estado' =>'RJ');
  105.         $where = array("id" => 15);
  106.         $mysql->execute('update', 'contato', $data, $where);
  107.         //EXCLUIR DADOS
  108.        
  109.         $where = array("id" => 4);
  110.         $mysql->execute('delete', 'contato', null, $where);
  111. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement