Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php/mysql] Klasa obsługująca MYSQL
ziom
post 8.03.2006, 13:24:03
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
nospor
post 8.03.2006, 13:26:12
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Ale czemu przedszkole? Jak juz cos to php winksmiley.jpg
Dalbym to na oceny, ale skoro to jeszcze nie jest skonczone...

edit: nie popadajmy w paranoję z tym przedszkolem winksmiley.jpg. Pytasz gdzies o manuala? nie. a wiec poziom wyzszy niz przedszkole smile.gif
(troche uogólnilem smile.gif )


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ziom
post 8.03.2006, 13:29:14
Post #3





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

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


Dałem to do przedszkola, bo uważam, że mniej więcej na tym poziomie to jest :]
Edit: No cóż smile.gif

Ten post edytował ziom 8.03.2006, 13:43:09


--------------------
biuro rachunkowe
Go to the top of the page
+Quote Post
Apo
post 8.03.2006, 14:50:45
Post #4





Grupa: Zarejestrowani
Postów: 426
Pomógł: 1
Dołączył: 2.10.2005

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


ja bym proponował do połączenia się z bazą danych użyć konstruktora i tak samo do rozłączania destruktora smile.gif
Go to the top of the page
+Quote Post
nickers
post 8.03.2006, 14:58:51
Post #5





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 6.02.2006
Skąd: Piła->Kotun();

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


Ja jednak pozostawił bym rozłączanie z bazą tak jak jest, czasem przeciez lepiej rozłączyć się wczesniej, by nie utrzymywać połączenia, które nie jest wykorzystywane.


Poza tym, w php jest destruktor?? smile.gif Jestem początkujący, ale w manualu chyba jest napisane, ze te nie istnieja, mozna co najwyzej je zasywulować.

-----
EDIT: Sprawdzilem w manualu jeszcze raz, jednak jest w PHP5 smile.gif.

Ten post edytował nickers 8.03.2006, 15:34:55


--------------------
Renesansowe Królestwa - gra RPG w średniowieczu. Zapraszam :)
Go to the top of the page
+Quote Post
ziom
post 8.03.2006, 15:53:26
Post #6





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

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


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

Cuś takiego ?

Ten post edytował ziom 8.03.2006, 16:41:31


--------------------
biuro rachunkowe
Go to the top of the page
+Quote Post
Apo
post 8.03.2006, 16:09:07
Post #7





Grupa: Zarejestrowani
Postów: 426
Pomógł: 1
Dołączył: 2.10.2005

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


TAK smile.gif Teraz musisz sobie zadać pytanie czy klasa ma być w php4 czy php5.
Z tego co mi wiadomo do destruktorów w php4 nie ma, a kostruktory są (nazwa metody taka sama jak nazwa klasy). Jeśli php5 to zmieć
var przy składowych na public, private, protected w zależności, i to samo przy metodach smile.gif

Cytat
Ja jednak pozostawił bym rozłączanie z bazą tak jak jest, czasem przeciez lepiej rozłączyć się wczesniej, by nie utrzymywać połączenia, które nie jest wykorzystywane.

To jest bez znaczenia, a dodatkowo używając destruktora nie musisz wyłowywać metody tylko sama sie wykonuje smile.gif

Ten post edytował Apo 8.03.2006, 16:12:30
Go to the top of the page
+Quote Post
ziom
post 8.03.2006, 16:43:09
Post #8





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

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


Dodałem tak jak mówiłem public itd.
Czy teraz jest dobzie? :]
No i zapytam czy jestem na dobrym tropie? tongue.gif


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

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 Wersja Lo-Fi Aktualny czas: 19.07.2025 - 19:26