Witam
Mam problem z klasą mysql, klasa wygląda tak:
<?php
class sql{
var $connection;
var $result;
var $rows;
var $queries = 0;
var $cache_state =0;
var $cache_file;
var $cache_buffer;
var $cache_ptr;
function sql_connect($host, $user, $pass, $db){
}
function sql_close(){
}
function sql_cache($handle = 0){
$this -> cache_state = 1;
$this -> cache_ptr = 0;
}else{
$this -> cache_state = 2;
$this -> cache_buffer
= array(); $this -> cache_file = CACHE_DIR.'cache_'.$handle.'.666';
}
}else{
if($this -> cache_state == 2){
file_put_contents
($this -> cache_file
, serialize($this -> cache_buffer
)); }
$this -> cache_state = 0;
}
}
function sql_cache_remove($handle){
unlink(CACHE_DIR
.'cache_'.$handle.'.666'); }
}
function sql_query($query){
if($this -> cache_state != 1){
$this -> queries++;
}
return 1;
}
}
function sql_fetch_array(){
if($this -> cache_state == 1){
if(!isset($this -> cache_buffer
[$this -> cache_ptr
])){ return 0;
}
$this -> rows = $this -> cache_buffer[$this -> cache_ptr];
$this -> cache_ptr++;
return 1;
}else{
if($this -> cache_state == 2){
// Dodaj do cache
$this -> cache_buffer[] = $this -> rows;
}
return 1;
}
}
return 0;
}
function sql_fetch_row(){
if($this -> cache_state == 1){
// czy koniec bufora?
if(!isset($this -> cache_buffer
[$this -> cache_ptr
])){ return 0;
}
// odczytaj z bufora
$this -> rows = $this -> cache_buffer[$this -> cache_ptr];
$this -> cache_ptr++;
return 1;
}else{
if($this -> cache_state == 2){
// Jeśli tworzymy cache, musimy rekord dodatkowo zapisac w buforze
$this -> cache_buffer[] = $this -> rows;
}
return 1;
}
}
return 0;
}
} // koniec klasy
?>
A samo wyświetlenie wyników następuje przez:
<?php
$sql = new sql;
$sql->sql_connect('localhost','user','haslo','baza');
$sql->sql_cache('download');
$sql->sql_query('SELECT * FROM tabela');
while($sql -> sql_fetch_array()){
echo $sql -> rows
[0].' - '.$sql -> rows
[1].'<br/>'; }
$sql->sql_cache();
$sql->sql_close();
?>
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'] ?