Location: PHPKode > projects > L-Forum > l-forum/lib/sql.inc
<?

if ( defined( '_SQL_INC' ) ) return;
define( '_SQL_INC', 1 );

class Database{
	var $conn;
	
	function Database($database=""){
	   global $db_name, $db_user, $db_pass, $db_host, $lang;
		if ($database==""){
			if (empty($db_name)) error_f("No database name");
			$database = $db_name;
		};
		$this->conn = pg_connect("host=$db_host dbname=$database user=$db_user password=$db_pass");
		if (!$this->conn) error_f($lang['database'].pg_errormessage());
	}
	
	function select($sql){
		return pg_exec($this->conn,$sql);
	}

	function nr($cos){
		return pg_numrows($cos);
	}
	function destroy(){
		pg_close($this->conn);
	}

	function get($index,$nr,$field){
		return @pg_result($index,$nr,$field);
	}
	function row($index,$nr){
		return @pg_fetch_array($index,$nr);
	}
};

class MySQLDatabase{
	var $conn;
	var $database;
	var $rowindex;
	
	function MySQLDatabase($database=""){
	   global $db_host, $db_name, $db_user, $db_pass, $lang;
		if ($database==""){
			if (empty($db_name)) error_f("No database name");
			$database = $db_name;
		};
		$this->conn = @mysql_connect($db_host, $db_user, $db_pass);
		$this->database = $database;
		if (!$this->conn) error_f($lang['database']."<br>".mysql_error());
		mysql_select_db($database);
		$this->rowindex = 0;
	}
	
	function select($sql){
		return mysql_query($sql,$this->conn);
	}

	function nr($cos){
		return mysql_num_rows($cos);
	}
	
	function affected(){
		return mysql_affected_rows($this->conn);
	}
	
	function destroy(){
		mysql_close($this->conn);
	}

	function get($index,$nr,$field){
		return mysql_result($index,$nr,$field);
	}
	function row($index,$nr){

		if ($this->rowindex!=$nr){
			mysql_data_seek($index,$nr);
			$this->rowindex = $nr;
		};
		$this->rowindex++;
		return mysql_fetch_array($index);
	}
};

class Query{
	var $index;
	var $length;
	var $database;
	var $_position;
	
	function Query($query,$database=""){
           global $lang;
           if(defined("DEBUG"))
              echo "$query<br>\n";
		if ($database==""){
			if (empty($GLOBALS["sys_database"]))
                           error_f($lang['database']);
			$database = $GLOBALS["sys_database"];
		};
		$this->database = $database;
		$this->index = $this->database->select($query);
		if (!$this->index)
                   error_f("<br>".$lang['query'].":<br>".$query);
		if (strtolower(substr($query,0,6))=="select")
			$this->length = $this->database->nr($this->index);
		$this->_position = 0;
	}

	function result(){
		return $this->index;
	}

	function get($i,$pole){
		return $this->database->get($this->index,$i,$pole);
	}

	function nr(){
		return $this->length;
	}
	
	function affected(){
		return $this->database->affected();
	}

	function movefirst(){
		$this->_position = 0;
		return !($this->eof());
	}
	function movenext(){
		$this->_position++;
		return !($this->eof());
	}
	function eof(){
		if ($this->_position>=$this->length) return true;
		else return false;
	}
	function moveprev(){
		$this->_position--;
	}
	function movelast(){
		$this->_position = $this->length-1;
	}
	function move($s){
		$this->_position = $s;
	}
	function val($pole){
		return $this->database->get($this->index,$this->_position,$pole);
	}
	function row(){
		$tmp = $this->database->row($this->index,$this->_position);
		$this->movenext();
		return $tmp;
	}
	function position(){
		return $this->_position;
	}
};
?>
Return current item: L-Forum