Location: PHPKode > scripts > mysql2object > mysql2object/datasource.class.php
<?
/**
 * DB Class - part of mysql2object set of classes, (modified DB class from lajt-mvc)
 *
 * @author Arkadiusz Mali&#324;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&#261;cy dane potrzebne do po&#322;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&#322;aczy&#263; si&#281; z serwerem baz danych. ".mysql_error());
		if(!mysql_select_db(substr($matches['path'], 1)))
			throw new DBException("Nie mozna po&#322;aczy&#263; si&#281; z baz&#261; danych. ".mysql_error());
	}


/**
 * destruktor klasy DB
 */
	public function __destruct() {
    	if(is_resource($this->_connection)) {
   			mysql_close($this->_connection);
        }
	}




	/**
	 * funkcja wykonanuj&#261;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&#322;ad zapytania SQL: ".$_sql.", ".mysql_error());
			}
		} else {
			if($wynik!=FALSE) {
				$this->_lastID = mysql_insert_id();
				return mysql_affected_rows();
			} else {
				throw new DBException("B&#322;ad zapytania SQL: ".$_sql.", ".mysql_error());
			}
		}
	}

	public function lastid() {
		return $this->_lastID;
	}

}


class DBException extends Exception {}

?>
Return current item: mysql2object