Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Abstrakcyjna klasa obslugi bazy danych
Beynar
post
Post #1





Grupa: Zarejestrowani
Postów: 60
Pomógł: 1
Dołączył: 6.12.2007

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


Napisalem abstrakycjna klase do obslugi bazy mysql
Co o niej sadzicie?
Chce na niej bazowac w duzym projekcie i nie chcialbym wtopic
Jak robic cachowanie? Nie mam pomyslu...
  1. <?php
  2.  
  3.  class db{
  4.  
  5.  public $_link; // connection id
  6.  public $_database; // selected db
  7.  public $_query; // current query
  8.  public $_resource; // data resource
  9.  public $_data; // data
  10.  public $_rows; // number of processed rows
  11.  public $_queries=0; // number of queris
  12.  public $_lastId; // last primary key
  13.  
  14.  
  15.  public function connect($host, $login, $pass, $_database){
  16.  if($_link=mysql_connect($host, $login, $pass))
  17.  $this -> _link = $_link;
  18.  else{
  19.  $this -> error();
  20.  return false;
  21.  }
  22.  if($_database=mysql_select_db($_database))
  23.  $this -> _database = $_database;
  24.  else{
  25.  $this -> error();
  26.  return false;
  27.  }
  28.  return true;
  29.  }
  30.  
  31.  
  32.  public function query($_query){
  33.  if(!$this -> _link || !$this -> _database){
  34.  $this -> error("can not do _query - no _database selected");
  35.  return false;
  36.  }
  37.  $this -> _query = $_query;
  38.  if($this -> _resource)
  39.  @mysql_free_result($this -> _resource);
  40.  if($_resource=mysql_query($_query, $this -> _link))
  41.  $this -> _resource = $_resource;
  42.  else{
  43.  $this -> error();
  44.  return false;
  45.  }
  46.  $this -> _queries++;
  47.  $this -> _lastId = mysql_insert_id($this -> _link);
  48.  return $this -> _resource;
  49.  }
  50.  
  51.  public function getData(){
  52.  if(!$this -> _link || !$this -> _database || !$this -> _resource){
  53.  $this -> error("can not get data from resource - no database selected or there is no data recosurce");
  54.  return false;
  55.  }
  56.  while($_data=mysql_fetch_array($this -> _resource, MYSQL_ASSOC))
  57.  $this -> _data[]=$_data;
  58.  if(!is_array($_data))
  59.  return $this -> _data;
  60.  else
  61.  $this -> error();
  62.  }
  63.  
  64.  public function getOnce(){
  65.  if(!$this -> _link || !$this -> _database || !$this -> _resource){
  66.  $this -> error("can not get data from resource - no database selected or there is no data recosurce");
  67.  return false;
  68.  }
  69.  if($_data=mysql_fetch_array($this -> _resource, MYSQL_ASSOC)){
  70.  $this -> _data[0]=$_data;
  71.  return true;
  72.  }
  73.  else
  74.  return false;
  75.  }
  76.  
  77.  public function numRows(){
  78.  if(!$this -> _link || !$this -> _database || !$this -> _resource){
  79.  $this -> error("can not get data from _resource because no _database selected or there is n
    o data recosurce"
    );
  80.  return false;
  81.  }
  82.  $_rows=mysql_num_rows($this -> _resource);
  83.  $this -> _rows = $_rows;
  84.  return $_rows;
  85.  }
  86.  
  87.  
  88.  public function flush(){
  89.  if($this -> _resource)
  90.  @mysql_free_result($this -> _resource);
  91.  unset($this -> _query);
  92.  unset($this -> _resource);
  93.  unset($this -> _data);
  94.  unset($this -> _rows);
  95.  $this -> _queries=0;
  96.  unset($this -> _lastId);
  97.  return true;
  98.  }
  99.  
  100.  private function error($error=false){
  101.  if($error)
  102.  echo "<p style=\"font-weight:bold;color:red;\">".$error."</p>";
  103.  echo "<p style=\"font-weight:bold;color:red\">".mysql_errno()." = ".mysql_error()."</p>";
  104.  if($this -> _query!="")
  105.  echo "<p style=\"font-weight:bold;color:red\">".$this -> _query."</p>";
  106.  }
  107.  
  108.  public function __destruct(){
  109.  $this -> flush();
  110.  if($this -> _link && $this -> _db){
  111.  if(!mysql_close($this -> _link)){
  112.  $this -> _error();
  113.  return false;
  114.  }
  115.  else
  116.  return true;
  117.  }
  118.  else 
  119.  return true;
  120.  }
  121.  }
  122.  ?>


Ten post edytował Beynar 11.12.2007, 22:59:41
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 26.12.2025 - 17:18