<?php
/**
================================================================================
LISENCE
================================================================================
This file is part of php4dvd.
php4dvd is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
php4dvd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with php4dvd. If not, see <http://www.gnu.org/licenses/>.
**/
//require_once(dirname(__FILE__) . "/DatabaseException.class.php");
require_once(dirname(__FILE__) . "/ResultSet.class.php");
/**
* Database object
*
* Database object connects to the database and executes queries on it
* PHP5 <
*
* @author Leslie Kleuver
*/
class Database {
var $UNKNOWN_DATABASE_ERROR = 100;
var $DATABASE_CONNECTION_ERROR = 101;
var $DATABASE_SELECTION_ERROR = 102;
var $SQL_SYNTAX_ERROR = 103;
var $db;
var $unbuffered;
var $counter; // counter for number of queries done
var $error = false;
var $errormessage = "Undefined error";
/**
* Database constructor
*
* @param string database username
* @param string database password
* @param string database name
* @param string database host
* @throws DatabaseException
* @return this
*/
function Database($settings) {
return $this->init($settings['user'], $settings['pass'], $settings['name'], $settings['host']);
}
function __destruct() {
@mysql_close($this->db);
}
/**
* Database initialization
*
* @param string database username
* @param string database password
* @param string database name
* @param string database host
* @throws DatabaseException
* @return this
*/
function init($user, $pasw, $name, $host) {
//initialize values to their defaults
$this->unbuffered = false;
//connect to database
$this->db = @mysql_connect($host, $user, $pasw) or $this->error = true and $this->errormessage = mysql_error();
$this->selectDatabase($name);
return $this;
}
/**
* Database selection
*
* @param string database name
* @throws DatabaseException
*/
function selectDatabase($name) {
@mysql_select_db($name, $this->db) or $this->error = true and $this->errormessage = mysql_error();
}
/**
* Query the database
*
* @param string sql query string
* @throws DatabaseException
* @return ResultSet
*/
function doQuery($sql) {
$this->counter++;
$result = $this->unbuffered ? @mysql_unbuffered_query($sql, $this->db) or $this->error = true and $this->errormessage = mysql_error() : @mysql_query($sql, $this->db) or $this->error = true and $this->errormessage = mysql_error();
return new ResultSet($result);
}
/**
* Alias for doQuery
*/
function select($sql) {
return $this->doQuery($sql);
}
/**
* Alias for doQuery
*/
function update($sql) {
return $this->doQuery($sql);
}
/**
* Alias for doQuery
*/
function insert($sql) {
return $this->doQuery($sql);
}
/**
* Alias for doQuery
*/
function delete($sql) {
return $this->doQuery($sql);
}
/**
* Get the insertId from the last query
*/
function insertId() {
return mysql_insert_id();
}
/**
* Get the insertId from the last query
*/
function numberOfAffectedRows() {
return mysql_affected_rows();
}
/**
* Get the amount of found rows
*/
function getFoundRows() {
$count = "SELECT FOUND_ROWS()";
$rs = $this->select($count);
$row = $rs->getNextRow();
return $row[0];
}
function escape($var)
{
return "'" . mysql_real_escape_string($var) . "'";
}
}
?>