MaTaXeToS

MySQL.class.php

Mar 15th, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.50 KB | None | 0 0
  1. <?php
  2.         define("DB_HOST", "localhost");
  3.         define("DB_USER", "root");
  4.         define("DB_PW", "");
  5.         define("DB_DATABASE", "test");
  6.        
  7.         class MySQL{
  8.                
  9.                 private $conexion;
  10.                 private $total_consultas;
  11.                 private $interno;
  12.                 public function __construct(){
  13.                         if(!isset($this->conexion)){
  14.                                 $this->conexion = (mysql_connect(DB_HOST,DB_USER,DB_PW)) or die(mysql_error());
  15.                                 mysql_select_db(DB_DATABASE,$this->conexion) or die(mysql_error());
  16.                         }
  17.                 }
  18.                 public function consulta($consulta, $interno){
  19.                         $resultado = mysql_query($consulta,$this->conexion);
  20.                         if(!$resultado){
  21.                                 echo 'MySQL Error: ' . mysql_error();
  22.                                 exit;
  23.                         }
  24.                         if ($interno != 1) {
  25.                                 $this->sumar_consultas();
  26.                         }
  27.                         return $resultado;
  28.                 }
  29.                 public function fetch_array($consulta){
  30.                         return mysql_fetch_array($consulta);
  31.                 }
  32.                
  33.                 public function fetch_object($consulta){
  34.                         return mysql_fetch_object($consulta);
  35.                 }
  36.                                
  37.                 public function num_rows($consulta){
  38.                         return mysql_num_rows($consulta);
  39.                 }
  40.                
  41.                 public function getTotalConsultas(){
  42.                         $consulta = $this->consulta('SELECT total FROM stats WHERE id="1"',1);
  43.                         $r = $this->fetch_array($consulta);
  44.                         return "Total consultas: " . $r['total'];
  45.                 }
  46.                 private function sumar_consultas($inc) {
  47.                         $consulta = $this->consulta('SELECT total FROM stats WHERE id="1"',1);
  48.                         $r = $this->fetch_array($consulta);
  49.                         $sumar = $r['total'] + $inc;
  50.                         $this->consulta('UPDATE stats SET total="'.$sumar.'" WHERE id="1"',1);
  51.                 }
  52.                
  53.                 function __destruct(){
  54.                         @mysql_free_result($this->consulta);
  55.                         @mysql_close($this->conexion);
  56.                 }
  57.         }
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment