Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php/mysql] Klasa obsługująca MYSQL
ziom
post
Post #1





Grupa: Zarejestrowani
Postów: 35
Pomógł: 0
Dołączył: 13.02.2005

Ostrzeżenie: (0%)
-----


  1. <?php
  2.  
  3. class  DB 
  4. {
  5.     var $cashe_dir = ''; //katalog, w którym składowane są cashe z danego pliku
  6.     var $bufor = array();
  7.     var $cashe_file = 'cashe'; //plik cashe
  8.  
  9.     var $conn;
  10.     var $db;
  11.     var $result;
  12.     
  13.     var $rows;
  14.     
  15.  
  16.     //funckcja sprawdzająca czy plik cashe isnieje
  17.     function cashe_exists($var)
  18.     {
  19.         if(file_exists($this->cashe_dir.$var))
  20.         {
  21.             return(true);
  22.         }
  23.         else
  24.         {
  25.             return(false);
  26.         }
  27.     }
  28.     //funckja otwierająca połączenie
  29.     function connect($host, $db_user, $db_pass, $db_name)
  30.     {
  31.         //sprawdzanie, czy zostały podane wszystkie dane
  32.         if(empty($host) OR empty($db_user) OR empty($db_pass) OR empty($db_name))
  33.         {
  34.             return(false);
  35.         }
  36.         else
  37.         {
  38.             $this -> conn = mysql_connect($host, $db_user,$db_pass);
  39.             $this -> db = mysql_select_db($db_name);
  40.         }
  41.     }
  42.     //funkcja zamykająca połączenie
  43.     function mysql_close_conn()
  44.     {
  45.         if(mysql_close($this -> conn))
  46.         {
  47.             return(true);
  48.         }
  49.         else
  50.         {
  51.             return(false);
  52.         }
  53.     }
  54.     //zapytanie
  55.     function sql_query($query)
  56.     {
  57.         //jezeli istnieje plik cashe to nie wykonuj zapytania
  58.         if(!$this->cashe_exists($this->cashe_file))
  59.         {
  60.             if(!empty($query))
  61.             {
  62.                 $this->result = mysql_query($query, $this->conn) or
  63.                 ($err = mysql_error());
  64.                 if( $this->result )
  65.                 {
  66.                     return( $this->result );
  67.                 }
  68.                 else
  69.                 {
  70.                     die($err);
  71.                     exit;
  72.                 }
  73.             }
  74.             else
  75.             {
  76.                 return(false);
  77.             }
  78.         }
  79.         else
  80.         {
  81.             return(1);
  82.         }
  83.     }
  84.     //zapytanie do tablicy
  85.     function sql_fetch_array($result, $mode = '0',$typ = '1')//Dostępne wartości to:1,2,3
  86.     {
  87.         $typy = array(
  88.             '1' => MYSQL_ASSOC,
  89.             '2' => MYSQL_NUM,
  90.             '3' => MYSQL_BOTH
  91.             );
  92.         //jeżeli istnieje plik cashe to pobierz z niego dane
  93.         if(!$this -> cashe_exists($this->cashe_file))
  94.         {
  95.             if($mode == '0')
  96.             {
  97.  
  98.                 while($rows = mysql_fetch_array($result,$typy[$typ]))
  99.                 {
  100.                     $this->bufor[] = $rows;
  101.                 }
  102.                 if($this->cashe_write($this->cashe_file))
  103.                 {
  104.                     return($this->bufor);
  105.                     unset($this->bufor);
  106.                 }
  107.                 else
  108.                 {
  109.                     return(false);
  110.                 }
  111.             }
  112.             elseif($mode == '1')
  113.             {
  114.                 while($rows = mysql_fetch_array($result,$typy[$typ]))
  115.                 {
  116.                     $this->bufor[] = $rows;
  117.                 }
  118.                 return($this->bufor);
  119.                 unset($this->bufor);
  120.                     
  121.             }
  122.         }
  123.         else
  124.         {
  125.             $this->bufor = unserialize(file_get_contents($this->cashe_dir.$this->cashe_file));
  126.             return($this->bufor);
  127.             unset($this->bufor);
  128.         }
  129.     }
  130.     //zapisuje do pliku cashe
  131.     function cashe_write()
  132.     {
  133.         //jeżeli plik cashe istnieje, zapisz, jeżeli nie zwróć false
  134.         if(!$this -> cashe_exists($this->cashe_file))
  135.         {
  136.             if(file_put_contents($this->cashe_dir.$this->cashe_file, serialize($this->bufor)))
  137.             {
  138.                 return(true);
  139.             }
  140.             else
  141.             {
  142.                 return(false);
  143.             }
  144.         }
  145.     }
  146.     //usuwa cashe
  147.     function delete_cashe()
  148.     {
  149.         //jeżeli plik cashe istnieje, usuń, jeżeli nie to zwróć false
  150.         if($this -> cashe_exists($this->cashe_file))
  151.         {
  152.             if(unlink($this->cashe_dir.$this->cashe_file))
  153.             {
  154.                 return(true);
  155.             }
  156.         }
  157.         else
  158.         {
  159.             return(false);
  160.         }
  161.     }
  162.  
  163.     function getFirstRecord($query)
  164.     {
  165.         $record = $this->sql_fetch_array($query,1,2);
  166.         return $record[0][0];
  167.     }
  168.  
  169.     function getFirstRow($query)
  170.     {
  171.         $row = $this->sql_fetch_array($query,1,3);
  172.         return $row[0];
  173.     }
  174.  
  175.     function getLastRecord($query)
  176.     {
  177.         $record = $this->sql_fetch_array($query,1,2);
  178.         $rows = count($record)-1;
  179.         $col = count($record[0])-1;
  180.         return $record[$rows][$col];
  181.     }
  182.     function getLastRow($query)
  183.     {
  184.         $record = $this->sql_fetch_array($query,1,3);
  185.         $row = count($record)-1;
  186.         return $record[$row];
  187.     }
  188.         
  189.     function Transaction ($var) {
  190.         
  191.         $mode = strtolower($var);
  192.         $array = array(
  193.             'fail' => 'ROLLBACK',
  194.             'complete' => 'COMMIT',
  195.             'start' => 'BEGIN'
  196.             );
  197.  
  198.         mysql_query($array[$mode]) or die(mysql_error());
  199.         
  200.     }
  201.     
  202.     function affected_rows()
  203.     {
  204.         return mysql_affected_rows();
  205.     }
  206.  
  207. }
  208.  
  209. ?>


Przykład użycia:
  1. <?php
  2.  
  3. $db = new DB;
  4. $db->cashe_dir = '';
  5. $db->cashe_file = 'xxx.666';
  6. $db->connect('host','dbusr', 'dbpass','dbname');
  7. $query = $db->sql_query('SELECT * FROM USERS WHERE NAME ='usr'');
  8. $array = $db->getFirstRow($query);
  9. print 'Name: '.$array['NAME'].'<br>';
  10. print 'Res: '.$array['RES'].'<BR>';
  11.  
  12. ?>


Zaznaczę tylko, żem początkujący więc głupie błędy są na miejscu smile.gif
Proszę o uwagi.Klasa jeszcze nie dokończona, chcę się dowiedzieć czy jestem na dobrym tropie smile.gif

Ten post edytował ziom 8.03.2006, 13:25:36


--------------------
biuro rachunkowe
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 20:29