class MGDb { private $connect_id; private $query_id = ""; private $query_row = array(); private $database = ""; private $queries = 0; function __construct($dbserver, $dbuser, $dbpassword, $db, $db_persistency = true, $debug = true) { $this->database = $db; $this->connect_id = ( $db_persistency ) ? @mysql_pconnect($dbserver, $dbuser, $dbpassword) : @mysql_connect($dbserver, $dbuser, $dbpassword); $this->debug = $debug; if ( !(@mysql_select_db($db, $this->connect_id)) ) { if( $this->connect_id ) { mysql_close($this->connect_id); } $this->sql_error("Imposible conectar a Database: " . $this->database); } } public function set_query($query = "", $line = "", $file = "") { unset($this->query_id); if( !($this->query_id = @mysql_query($query, $this->connect_id)) ) { $msg_exts = "Consulta: " . $query . "
SQL error: " . mysql_error($this->connect_id) . "
SQL error code: " . mysql_errno($this->connect_id) . "
Archivo: " . $file . "
Linea:" . $line ; $this->sql_error("Error en consulta", $msg_exts); } $this->queries++; return $this->query_id; } public function fetchrow($query_id = "") { if($query_id == "") { $query_id = $this->query_id; } $this->query_row = mysql_fetch_array($query_id, MYSQL_ASSOC); return ( $query_id ) ? $this->query_row : false; } public function fetchobject($query_id = "") { if($query_id == "") { $query_id = $this->query_id; } $this->query_row = mysql_fetch_object($query_id); return ( $query_id ) ? $this->query_row : false; } // Borrowed from phpBB public function fetchrowset($query_id = "") { if($query_id == "") { $query_id = $this->query_id; } if($query_id) { unset($rowset[$query_id]); unset($row[$query_id]); while($rowset[$query_id] = @mysql_fetch_array($query_id)) { $result[] = $rowset[$query_id]; } return $result; } else { return false; } } public function num_queries() { return $this->queries; } public function get_database() { return $this->database; } public function count_rows($row, $table, $where) { $result = $this->set_query("SELECT COUNT(" . $row . ") AS total FROM " . $table . " " . $where ); $row = $this->fetchrow($result); return $row['total']; } public function get_version() { return mysql_get_server_info(); } // Functions MySQL public function num_rows($query_id = "") { if( query_id == "" ) { $query_id = $this->query_id; } return ( $query_id ) ? mysql_num_rows($query_id) : false; } public function free_result($query_id = "") { if ($query_id == "") { $query_id = $this->query_id; } unset($this->query_row); return ( !(@mysql_free_result($query_id)) ) ? false: true; } public function affected_rows() { return ( $this->connect_id ) ? mysql_affected_rows($this->connect_id) : false; } public function close() { if( $this->query_id ) { @mysql_free_result($this->query_id); } return ( $this->connect_id ) ? mysql_close($this->connect_id) : false; } public function num_fields($query_id = "") { if( $query_id == "" ) { $query_id = $this->query_id; } return ( $query_id ) ? mysql_num_fields($query_id) : false; } public function field_name($offset, $query_id = "") { if( $query_id == "") { $query_id = $this->query_id; } return ( $query_id ) ? mysql_field_name($query_id, $offset) : false; } public function field_type($offset, $query_id = "") { if( $query_id == "") { $query_id = $this->query_id; } return ( $query_id ) ? mysql_field_type($query_id, $offset) : false; } public function data_seek($num, $query_id = "") { if( $query_id == "" ) { $query_id = $this->query_id; } return ( $query_id ) ? mysql_data_seek($query_id, $num) : false; } public function insert_id() { return ( $this->connect_id ) ? mysql_insert_id($this->connect_id) : false; } public function list_tables($database = "") { if( $database == "" ) { $database = $this->database; } return ( $database ) ? mysql_list_tables($this->database) : false; } public function list_fields($table, $database = "") { if( $database == "" ) { $database = $this->database; } $result = $GLOBALS['db']->set_query("SHOW COLUMNS FROM " . $table, __LINE__, __FILE__); return $result; } public function tablename($tables = "", $pointer = 0) { if( $tables == "" ) { $tables = $this->list_tables(); } return mysql_tablename($tables, $pointer); } private function sql_error($msg, $exp = "") { $msg_die = ( $this->debug ) ? $msg . "
" . $exp : $msg; if( defined('MG_DIR') ) { info(ERROR, $msg_die); } else { die($msg_die); } } public function set_query_return($query = "") { unset($this->query_id); if( !($this->query_id = @mysql_query($query, $this->connect_id)) ) { return $sql_error = array( 'query' => $query, 'error' => mysql_error($this->connect_id) ); } return $this->query_id; } } ?>