| | |
| | | |
| | | <?php |
| | | /* |
| | | Copyright (c) 2005, Till Brehm, projektfarm Gmbh |
| | |
| | | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| | | */ |
| | | |
| | | class db |
| | | class db extends mysqli |
| | | { |
| | | var $dbHost = ''; // hostname of the MySQL server |
| | | var $dbName = ''; // logical database name on that server |
| | | var $dbUser = ''; // database authorized user |
| | | var $dbPass = ''; // user's password |
| | | var $dbCharset = 'utf8';// Database charset |
| | | var $dbNewLink = false; // Return a new linkID when connect is called again |
| | | var $dbClientFlags = 0; // MySQL Client falgs |
| | | var $linkId = 0; // last result of mysql_connect() |
| | | var $queryId = 0; // last result of mysql_query() |
| | | var $record = array(); // last record fetched |
| | | var $autoCommit = 1; // Autocommit Transactions |
| | | var $currentRow; // current row number |
| | | var $errorNumber = 0; // last error number |
| | | var $errorMessage = ''; // last error message |
| | | var $errorLocation = '';// last error location |
| | | var $show_error_messages = true; |
| | | private $dbHost = ''; // hostname of the MySQL server |
| | | private $dbName = ''; // logical database name on that server |
| | | private $dbUser = ''; // database authorized user |
| | | private $dbPass = ''; // user's password |
| | | private $dbCharset = 'utf8';// Database charset |
| | | private $dbNewLink = false; // Return a new linkID when connect is called again |
| | | private $dbClientFlags = 0; // MySQL Client falgs |
| | | private $linkId = 0; // last result of mysqli_connect() |
| | | private $queryId = 0; // last result of mysqli_query() |
| | | private $record = array(); // last record fetched |
| | | private $autoCommit = 1; // Autocommit Transactions |
| | | private $currentRow; // current row number |
| | | private $errorNumber = 0; // last error number |
| | | public $errorMessage = ''; // last error message |
| | | private $errorLocation = '';// last error location |
| | | public $show_error_messages = false; // false in server, true in interface |
| | | |
| | | // constructor |
| | | public function __construct() { |
| | | |
| | | global $conf; |
| | | $this->dbHost = $conf['db_host']; |
| | | $this->dbName = $conf['db_database']; |
| | |
| | | $this->dbCharset = $conf['db_charset']; |
| | | $this->dbNewLink = $conf['db_new_link']; |
| | | $this->dbClientFlags = $conf['db_client_flags']; |
| | | //$this->connect(); |
| | | parent::__construct($conf['db_host'], $conf['db_user'],$conf['db_password'],$conf['db_database']); |
| | | if ($this->connect_error) { |
| | | $this->updateError('DB::__construct'); |
| | | return false; |
| | | } |
| | | parent::query( 'SET NAMES '.$this->dbCharset); |
| | | parent::query( "SET character_set_results = '".$this->dbCharset."', character_set_client = '".$this->dbCharset."', character_set_connection = '".$this->dbCharset."', character_set_database = '".$this->dbCharset."', character_set_server = '".$this->dbCharset."'"); |
| | | |
| | | } |
| | | |
| | | public function __destruct() { |
| | | $this->closeConn(); |
| | | $this->close(); // helps avoid memory leaks, and persitent connections that don't go away. |
| | | } |
| | | |
| | | // error handler |
| | | function updateError($location) |
| | | { |
| | | public function updateError($location) { |
| | | global $app; |
| | | $this->errorNumber = @mysql_errno($this->linkId); |
| | | $this->errorMessage = @mysql_error($this->linkId); |
| | | |
| | | if($this->connect_error) { |
| | | $this->errorNumber = $this->connect_errno; |
| | | $this->errorMessage = $this->connect_error; |
| | | } else { |
| | | $this->errorNumber = $this->errno; |
| | | $this->errorMessage = $this->error; |
| | | } |
| | | |
| | | $this->errorLocation = $location; |
| | | if($this->errorNumber && $this->show_error_messages && method_exists($app,'log')) |
| | | { |
| | | // echo('<br /><b>'.$this->errorLocation.'</b><br />'.$this->errorMessage); |
| | | $app->log($this->errorLocation.' '.$this->errorMessage,LOGLEVEL_WARN); |
| | | //flush(); |
| | | if($this->errorNumber) { |
| | | $error_msg = $this->errorLocation .' '. $this->errorMessage; |
| | | // This right here will allow us to use the samefile for server & interface |
| | | if($this->show_error_messages) { |
| | | echo $error_msg; |
| | | } else if(method_exists($app, 'log')) { |
| | | $app->log($error_msg, LOGLEVEL_WARN); |
| | | } |
| | | } |
| | | } |
| | | |
| | | function connect() |
| | | { |
| | | if($this->linkId == 0) |
| | | { |
| | | $this->linkId = @mysql_connect($this->dbHost, $this->dbUser, $this->dbPass, $this->dbNewLink, $this->dbClientFlags); |
| | | if(!$this->linkId) |
| | | { |
| | | $this->updateError('DB::connect()-> mysql_connect'); |
| | | return false; |
| | | } |
| | | $this->queryId = @mysql_query('SET NAMES '.$this->dbCharset, $this->linkId); |
| | | $this->queryId = @mysql_query("SET character_set_results = '".$this->dbCharset."', character_set_client = '".$this->dbCharset."', character_set_connection = '".$this->dbCharset."', character_set_database = '".$this->dbCharset."', character_set_server = '".$this->dbCharset."'", $this->linkId); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | function query($queryString) |
| | | { |
| | | if(!$this->connect()) |
| | | { |
| | | return false; |
| | | } |
| | | if(!mysql_select_db($this->dbName, $this->linkId)) |
| | | { |
| | | $this->updateError('DB::connect()-> mysql_select_db'); |
| | | return false; |
| | | } |
| | | $this->queryId = @mysql_query($queryString, $this->linkId); |
| | | $this->updateError('DB::query('.$queryString.') -> mysql_query'); |
| | | if(!$this->queryId) |
| | | { |
| | | public function query($queryString) { |
| | | $this->queryId = parent::query($queryString); |
| | | $this->updateError('DB::query('.$queryString.') -> mysqli_query'); |
| | | if(!$this->queryId) { |
| | | return false; |
| | | } |
| | | $this->currentRow = 0; |
| | |
| | | } |
| | | |
| | | // returns all records in an array |
| | | function queryAllRecords($queryString) |
| | | { |
| | | public function queryAllRecords($queryString) { |
| | | if(!$this->query($queryString)) |
| | | { |
| | | return false; |
| | |
| | | } |
| | | |
| | | // returns one record in an array |
| | | function queryOneRecord($queryString) |
| | | { |
| | | public function queryOneRecord($queryString) { |
| | | if(!$this->query($queryString) || $this->numRows() == 0) |
| | | { |
| | | return false; |
| | |
| | | } |
| | | |
| | | // returns the next record in an array |
| | | function nextRecord() |
| | | { |
| | | $this->record = mysql_fetch_assoc($this->queryId); |
| | | public function nextRecord() { |
| | | $this->record = $this->queryId->fetch_assoc(); |
| | | $this->updateError('DB::nextRecord()-> mysql_fetch_array'); |
| | | if(!$this->record || !is_array($this->record)) |
| | | { |
| | |
| | | } |
| | | |
| | | // returns number of rows returned by the last select query |
| | | function numRows() |
| | | { |
| | | return mysql_num_rows($this->queryId); |
| | | public function numRows() { |
| | | return $this->queryId->num_rows; |
| | | } |
| | | |
| | | // returns mySQL insert id |
| | | function insertID() |
| | | { |
| | | return mysql_insert_id($this->linkId); |
| | | public function insertID() { |
| | | return $this->insert_id; |
| | | } |
| | | |
| | | |
| | | // Check der variablen |
| | | // Really.. using quote should be phased out in favor of using bind_param's. Though, for legacy code.. |
| | | // here's the equivalent |
| | | public function quote($formfield) { |
| | | return $this->escape_string($formfield); |
| | | } |
| | | |
| | | // Check der variablen |
| | | // deprecated, now use quote |
| | | function check($formfield) |
| | | { |
| | | return $this->quote($formfield); |
| | | } |
| | | |
| | | // Check der variablen |
| | | function quote($formfield) |
| | | { |
| | | if(!$this->connect()){ |
| | | $this->updateError('WARNING: mysql_connect: Used addslashes instead of mysql_real_escape_string'); |
| | | return addslashes($formfield); |
| | | } |
| | | |
| | | return mysql_real_escape_string($formfield, $this->linkId); |
| | | } |
| | | |
| | | // Check der variablen |
| | | function unquote($formfield) |
| | | { |
| | | public function unquote($formfield) { |
| | | return stripslashes($formfield); |
| | | } |
| | | |
| | | function toLower($record) { |
| | | public function toLower($record) { |
| | | if(is_array($record)) { |
| | | foreach($record as $key => $val) { |
| | | $key = strtolower($key); |
| | |
| | | } |
| | | |
| | | |
| | | public function closeConn() |
| | | { |
| | | if($this->linkId) |
| | | { |
| | | mysql_close($this->linkId); |
| | | return true; |
| | | } else { return false; } |
| | | } |
| | | |
| | | public function freeResult($query) |
| | | { |
| | | if(mysql_free_result($query)) |
| | | { |
| | | if(is_object($query) && (get_class($query) == "mysqli_result")) { |
| | | $query->free(); |
| | | return true; |
| | | } else { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | function delete() { |
| | | /* TODO: Does anything use this? */ |
| | | public function delete() { |
| | | |
| | | } |
| | | |
| | | function Transaction($action) { |
| | | /* TODO: Does anything use this? */ |
| | | public function Transaction($action) { |
| | | //action = begin, commit oder rollback |
| | | |
| | | } |
| | |
| | | |
| | | */ |
| | | |
| | | function createTable($table_name,$columns) { |
| | | public function createTable($table_name,$columns) { |
| | | $index = ''; |
| | | $sql = "CREATE TABLE $table_name ("; |
| | | foreach($columns as $col){ |
| | |
| | | |
| | | |
| | | */ |
| | | function alterTable($table_name,$columns) { |
| | | public function alterTable($table_name,$columns) { |
| | | $index = ''; |
| | | $sql = "ALTER TABLE $table_name "; |
| | | foreach($columns as $col){ |
| | |
| | | return true; |
| | | } |
| | | |
| | | function dropTable($table_name) { |
| | | public function dropTable($table_name) { |
| | | $this->check($table_name); |
| | | $sql = "DROP TABLE '". $table_name."'"; |
| | | return $this->query($sql); |
| | | } |
| | | |
| | | // gibt Array mit Tabellennamen zur�ck |
| | | function getTables($database_name = '') { |
| | | public function getTables($database_name = '') { |
| | | |
| | | if($database_name == '') $database_name = $this->dbName; |
| | | $result = mysql_list_tables($database_name); |
| | | for ($i = 0; $i < mysql_num_rows($result); $i++) { |
| | | $tb_names[$i] = mysql_tablename($result, $i); |
| | | $result = parent::query("SHOW TABLES FROM $database_name"); |
| | | for ($i = 0; $i < $result->num_rows; $i++) { |
| | | $tb_names[$i] = (($result->data_seek( $i) && (($___mysqli_tmp = $result->fetch_row()) !== NULL)) ? array_shift($___mysqli_tmp) : false); |
| | | } |
| | | return $tb_names; |
| | | } |
| | |
| | | |
| | | } |
| | | |
| | | function mapType($metaType,$typeValue) { |
| | | public function mapType($metaType,$typeValue) { |
| | | global $go_api; |
| | | $metaType = strtolower($metaType); |
| | | switch ($metaType) { |