Witam,
Jestem początkujący w php5, Przeszukałem neta i znalazłem parę przydatnych rzeczy skleiłem je w całość w wyszła cała klasa. Jednak nie wiem czy jest ona dobrze napisana, mógł by mi to ktoś sprawdzić ? Poniżej załanczam klasę. Z góry dziękuje za pomoc.
<?
# ----------------------------------------
# Sterownik bazy MySQL
# ----------------------------------------
define("CACHE_DIR", "./sql_cache/");
class Sql
{
public $cid;
public $queryResult = array(); public $queries = 0;
public $rows = array(0
=>array
()); public $cache_state = 0;
public $cache_file;
public $cache_buffer;
public $cache_ptr;
public function __construct($host, $username, $password, $database)
{
$this -> cid
= mysql_connect($host, $username, $password) or
die ($this -> error
("Problem z nawiazaniem polaczenia")); mysql_select_db($database, $this -> cid
) or
die ($this -> error
("Problem z wybraniem bazy")); } // end __construct();
public function sqlClose()
{
} // end sqlClose();
public function sqlCache($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.'.sql';
}
}
else
{
if($this -> cache_state == 2)
{
file_put_contents
($this -> cache_file
, serialize($this -> cache_buffer
)); }
$this -> cache_state = 0;
}
} // end sql_cache();
public function sqlCacheRemove($handle)
{
{
unlink(CACHE_DIR
.'cache_'.$handle.'.sql'); }
} // end sql_cache_remove();
public function error($what = "Main System Error With Data Base")
{
return "<p style=\"color: red; font-weight: bold\">Driver SQL Problem <br>" . $what . " </p> ".mysql_error(); } // end error();
public function query($to_query, $query_name)
{
if($this -> cache_state != 1)
{
$this -> queryResult
[$query_name] = mysql_query($to_query, $this -> cid
); $this -> queries++;
return 1;
}
} // end query();
public function numRows($query_name)
{
} // end numRows();
public function fetchRow($query_name)
{
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 -> rows
[$query_name] = mysql_fetch_row($this -> queryResult
[$query_name])) {
if($this -> cache_state == 2)
{
$this -> cache_buffer[] = $this -> rows;
}
return 1;
}
}
return 0;
} // end fetchRow();
public function fetchAssoc($query_name)
{
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 -> rows
[$query_name] = mysql_fetch_assoc($this -> queryResult
[$query_name])) {
if($this -> cache_state == 2)
{
$this -> cache_buffer[] = $this -> rows;
}
return 1;
}
}
return 0;
} // end fetchAssoc();
public function insertId()
{
} // edn insertId();
public function affectedRows()
{
} // end affectedRows();
public function fetchQuery($to_query, $query_name)
{
$this -> queryResult
[$query_name] = mysql_query($to_query, $this -> cid
); $this -> queries++;
} // end fetchQuery();
}
?>
Powód edycji: zmieniłem bbcode (cysiaczek)