Zebrałem się w sobie i napisałem prostą klasę do obsługi MySQLa. Interesuje mnie czy to jest dobrze napisane, ewentualnie co zmienić. Klasa pod PHP4.

Kod:

  1. <?php
  2.  
  3. class db {
  4.  var $zapytania = 0;
  5.  var $conn = false;
  6.  var $errors = 0;
  7.  var $debug = false;
  8.  var $identyfikator = false;
  9.  
  10.  function err($errtxt, $errno){
  11.   $this->errors++;
  12.   if($this->debug) {
  13.   echo "MySQL zwrócił błąd numer:".$errno.". O treści:".$errtxt.". Zapytanie nie mogło zostać wykonane";
  14.   }
  15.  }
  16.  
  17.  function connect($host, $user, $pass, $baza){
  18.   $connection = mysql_connect($host, $user, $pass);
  19.   $bazaselect = mysql_select_db($baza, $connection);
  20.   
  21.   if(!$connection){
  22.   $this->err($mysql_error, $mysql_errno);
  23.   } else {
  24.   $this->conn = true;
  25.   }
  26.   
  27.   if(!$bazaselect){
  28.   $this->err($mysql_error, $mysql_errno);
  29.   $this->conn = false;
  30.   } else {
  31.   $this->identyfikator = $connection;
  32.   }
  33.  }
  34.  
  35.  function query($query){
  36.   $doquery = mysql_query($query);
  37.   
  38.   if(!$doquery) {
  39.   $this->err($mysql_error, $mysql_errno);
  40.   } else {
  41.   $this->zapytania++;
  42. return $doquery;
  43.   }
  44.  }
  45.  
  46.  function close(){
  47.   mysql_close($this->identyfikator);
  48.   $this->conn = false;
  49.   $this->identyfikator = false;
  50.  }
  51. }
  52.  
  53. ?>


Dalej chciałbym normalnie operować na zapytaniu, np. mysql_fetch_assoc itp.