Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Klasa mysql
Forum PHP.pl > Forum > PHP
Plikownik
Witam

Mam problem z klasą mysql, klasa wygląda tak:
  1. <?php
  2.  
  3.  class sql{
  4. var $connection;
  5. var $result;
  6. var $rows;
  7.  
  8. var $queries = 0;
  9.  
  10. var $cache_state =0;
  11. var $cache_file;
  12. var $cache_buffer;
  13. var $cache_ptr;
  14.  
  15. function sql_connect($host, $user, $pass, $db){
  16.  $this -> connection = mysql_connect($host, $user, $pass);
  17. }
  18.  
  19. function sql_close(){
  20.  mysql_close($this -> connection);
  21. }
  22. function sql_cache($handle = 0){
  23.  if(is_string($handle)){
  24. if(file_exists(CACHE_DIR.'cache_'.$handle.'.666')){
  25.  $this -> cache_state  = 1;
  26.  $this -> cache_ptr = 0;
  27.  $this -> cache_buffer = unserialize(file_get_contents(CACHE_DIR.'cache_'.$handle.'.666'));
  28. }else{
  29.  $this -> cache_state = 2;
  30.  $this -> cache_buffer = array();
  31.  $this -> cache_file = CACHE_DIR.'cache_'.$handle.'.666';
  32. }
  33.  }else{
  34. if($this -> cache_state == 2){
  35.  file_put_contents($this -> cache_file, serialize($this -> cache_buffer));
  36. }
  37. $this -> cache_state = 0;
  38.  }
  39. }
  40.  
  41. function sql_cache_remove($handle){
  42.  if(file_exists(CACHE_DIR.'cache_'.$handle.'.666')){
  43. unlink(CACHE_DIR.'cache_'.$handle.'.666');
  44.  }
  45. }
  46. function sql_query($query){
  47.  if($this -> cache_state != 1){
  48. $this -> result = mysql_query($query);
  49. $this -> queries++;
  50.  
  51. if(mysql_errno() != 0){
  52.  die('Błąd: '.mysql_error().'<br/>');
  53. }
  54. return 1;
  55.  }
  56. }
  57. function sql_fetch_array(){
  58.  if($this -> cache_state == 1){
  59. if(!isset($this -> cache_buffer[$this -> cache_ptr])){
  60.  return 0;
  61. }
  62. $this -> rows = $this -> cache_buffer[$this -> cache_ptr];
  63. $this -> cache_ptr++;
  64. return 1;
  65.  }else{
  66. if($this -> rows = mysql_fetch_assoc($this -> result)){
  67.  if($this -> cache_state == 2){
  68. // Dodaj do cache
  69. $this -> cache_buffer[] = $this -> rows;
  70.  }
  71.  return 1;
  72. }
  73.  }
  74.  return 0;
  75. }
  76.  
  77. function sql_fetch_row(){
  78.  if($this -> cache_state == 1){
  79. // czy koniec bufora?
  80. if(!isset($this -> cache_buffer[$this -> cache_ptr])){
  81.  return 0;
  82. }
  83. // odczytaj z bufora
  84. $this -> rows = $this -> cache_buffer[$this -> cache_ptr];
  85. $this -> cache_ptr++;
  86. return 1;
  87.  }else{
  88. if($this -> rows = mysql_fetch_array($this -> result)){
  89.  if($this -> cache_state == 2){
  90. // Jeśli tworzymy cache, musimy rekord dodatkowo zapisac w buforze
  91. $this -> cache_buffer[] = $this -> rows;
  92.  }
  93.  return 1;
  94. }
  95.  }
  96.  return 0;
  97. }
  98.  
  99.  } // koniec klasy
  100.  
  101. ?>


A samo wyświetlenie wyników następuje przez:
  1. <?php
  2.  
  3. $sql = new sql;
  4. $sql->sql_connect('localhost','user','haslo','baza');  
  5.  
  6.  $sql->sql_cache('download');
  7.  $sql->sql_query('SELECT * FROM tabela');
  8.  
  9.  while($sql -> sql_fetch_array()){
  10. echo $sql -> rows[0].' - '.$sql -> rows[1].'<br/>';
  11.  }
  12.  $sql->sql_cache();
  13.  $sql->sql_close();
  14.  
  15. ?>


W pętli widać, że muszę wpisać $sql->rows[0] aby otrzymać wynik, jak zmodyfikować klasę, aby zamiast $sql->rows[0] było np. $sql->rows['id'] ?
TomASS
Nie wiem w czym masz problem - u mnie wszystko ok:
  1. <?php
  2.  
  3.  $sql = new sql;
  4. $sql->sql_connect('localhost','root','','euro');  
  5.  
  6.  $sql->sql_cache('download');
  7.  $sql->sql_query('SELECT * FROM euro_towar');
  8.  
  9.  while($sql -> sql_fetch_array()){
  10.  //print_r($sql);  //zobacz co wyswietla
  11. echo $sql -> rows['Nazwa'].' - '.$sql -> rows['Jednostki'].'<br/>';
  12.  }
  13.  $sql->sql_cache();
  14.  $sql->sql_close();
  15.  
  16. ?>


Cytat
blacha - arkusze
ruda - kg
węgiel - kg
Bastion
na chlopski rozum ja to widze tak smile.gif

- scacheowalo ci pusty wynik zakladajac cache "download"
potem moze dales jakies dane do tabeli nie czyszczac cache'u
dlatego query zwracac moze pusty wynik zapisany w cache ..
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.