Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.68 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', '', 'videoaulasbrasil');
  87. $bd->connect();
  88.  
  89. //PARA INSERIR
  90. $data = array('categoria' => 'Informática', 'texto' => 'Tagtagatag');
  91. $bd->insert('categorias', $data);
  92.  
  93.  
  94. //PARA ALTERAR
  95. $data = array('categoria' => 'Informática 2 ', 'texto' => 'Tagtagatag 2');
  96. $bd->update('categorias', $data, "id = 5");
  97.  
  98.  
  99. //PARA EXCLUIR
  100. $bd->delete('categorias', "id = 5");
  101.  
  102.  
  103. ///Lista todo os itens
  104. $strSQL = "SELECT * FROM categorias";
  105. foreach ($bd->fetchAll($strSQL) as $k => $v){
  106.         echo $v['categoria'];
  107. }
  108.  
  109. //Retornar apenas uma linha
  110. $strSQL = "SELECT * FROM categorias WHERE id = 4";
  111. $row  = $bd->fetchOne($strSQL);
  112. echo $row['categoria'];
  113.  
  114. //Total de Registro
  115. $strSQL = "SELECT * FROM categorias";
  116. echo $bd->numRows($strSQL);
  117.  
  118.  
  119.  
  120.  
  121. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement