<?
/**
* DB Class - part of mysql2object set of classes, (modified DB class from lajt-mvc)
*
* @author Arkadiusz Maliński <hide@address.com>
* @package mysql2object
*/
class DB {
private $_connection;
private $_typ;
private $_lastID = 0;
public $_debug;
/**
* konstruktor klasy DB
*
* @param string DSN - string zawierający dane potrzebne do połaczenia w formacie 'typ://uzytkownik:hide@address.com/nazwa_bazy'
*/
public function __construct($dsn = "mysql://user:hide@address.com/db") {
$matches = parse_url($dsn);
$this->_typ = $matches['scheme'];
$this->_connection = @mysql_connect($matches['host'], $matches['user'], $matches['pass']);
if(!is_resource($this->_connection))
throw new DBException("Nie mozna połaczyć się z serwerem baz danych. ".mysql_error());
if(!mysql_select_db(substr($matches['path'], 1)))
throw new DBException("Nie mozna połaczyć się z bazą danych. ".mysql_error());
}
/**
* destruktor klasy DB
*/
public function __destruct() {
if(is_resource($this->_connection)) {
mysql_close($this->_connection);
}
}
/**
* funkcja wykonanująca zapytania SQL do bazy
*
* @param string zapytanie SQL
* @return array
*/
public function query($sql) {
$_sql = trim($sql);
$wynik = mysql_query($_sql, $this->_connection);
if(preg_match('/(SELECT|SHOW|DESCRIBE|EXPLAIN)/', $sql)) {
if($wynik!=FALSE) {
$tmpArr = array();
while($_row = mysql_fetch_array($wynik, MYSQL_BOTH)) {
array_push($tmpArr, $_row);
}
return $tmpArr;
} else {
throw new DBException("Bład zapytania SQL: ".$_sql.", ".mysql_error());
}
} else {
if($wynik!=FALSE) {
$this->_lastID = mysql_insert_id();
return mysql_affected_rows();
} else {
throw new DBException("Bład zapytania SQL: ".$_sql.", ".mysql_error());
}
}
}
public function lastid() {
return $this->_lastID;
}
}
class DBException extends Exception {}
?>