Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- /**
- * database layer for mysql
- */
- class db
- {
- /**
- * @var string database
- * @access private
- */
- var $database = "";
- /**
- * @var string server
- * @access private
- */
- var $server = "";
- /**
- * @var string user
- * @access private
- */
- var $user = "";
- /**
- * @var string password
- * @access private
- */
- var $password = "";
- /**
- * @var string tableprefix
- * @access private
- */
- var $tableprefix = "webpm_";
- /**
- * @var resource link_id stores the mysql link identifier
- * @access private
- */
- var $link_id = 0;
- /**
- * @var integer querycounter
- * @access private
- */
- var $querycounter = 0;
- /**
- * @var array queries
- * @access private
- */
- var $queries=array();
- /**
- * constructor
- *
- * @param string server
- * @param string user
- * @param string password
- * @param string database
- * @param string tableprefix
- */
- function db($server,$user,$password,$database,$tableprefix)
- {
- $this->server=$server;
- $this->user=$user;
- $this->password=$password;
- $this->database=$database;
- $this->tableprefix=$tableprefix;
- $this->querycounter=0;
- $this->queries=array();
- $this->querytimer=array();
- $this->link_id=0;
- $this->connect();
- }
- /**
- * open a connection to the database server
- *
- * @return void
- * @access private
- */
- function connect()
- {
- if($this->link_id==0)
- {
- $this->link_id=mysql_connect($this->server,$this->user,$this->password);
- if(!$this->link_id)
- {
- $this->error("cannot connect to database server ".$this->server);
- }
- if($this->database!="")
- {
- $this->select_db();
- }
- }
- }
- /**
- * selects a database
- *
- * @param string databse
- * @return void
- * @access public
- */
- function select_db($database="")
- {
- if($database!="")
- {
- $this->database=$database;
- }
- if(!@mysql_select_db($this->database, $this->link_id))
- {
- $this->error("cannot use database ".$this->database);
- }
- }
- /**
- * sends and execute a sql query
- *
- * @param string query_string
- * @return resource result
- * @access public
- */
- function query($query_string)
- {
- $result = mysql_query($query_string,$this->link_id);
- $this->queries[]=$query_string;
- if(!$result)
- {
- $this->error("Invalid SQL: ".$query_string);
- }
- $this->querycounter++;
- return $result;
- }
- /**
- * fetches a result row as an associative array, a numeric array, or both
- *
- * @param resource result
- * @param integer result_type
- * @return array result row
- * @access public
- */
- function fetch_array($result,$result_type=MYSQL_BOTH)
- {
- return @mysql_fetch_array($result,$result_type);
- }
- /**
- * free result memory
- *
- * @param resource result
- * @return boolean
- * @access public
- */
- function free_result($result)
- {
- return @mysql_free_result($result);
- }
- /**
- * sends a sql query and fetches the first result row as an associative array, a numeric array, or both
- *
- * @param string query_string
- * @param integer result_type
- * @return array result row
- * @access public
- */
- function query_first($query_string,$result_type=MYSQL_BOTH)
- {
- $result = $this->query($query_string);
- $returnarray=$this->fetch_array($result,$result_type);
- $this->free_result($result);
- return $returnarray;
- }
- /**
- * sends an SQL query to MySQL, without fetching and buffering the result rows
- *
- * @param string query_string
- * @param integer low_priority
- * @return resource result
- * @access public
- */
- function unbuffered_query($query_string,$low_priority=0)
- {
- global $phpversion;
- if($phpversion<406)
- {
- return $this->query($query_string);
- }
- else
- {
- if($low_priority==1)
- {
- $query_string=preg_replace("/^(REPLACE|INSERT|UPDATE|DELETE)/im","\\1 LOW_PRIORITY", $query_string);
- }
- $result = mysql_unbuffered_query($query_string,$this->link_id);
- $this->queries[]="unbuffered: $query_string";
- if(!$result) $this->error("Invalid SQL: ".$query_string);
- $this->querycounter++;
- return $result;
- }
- }
- /**
- * returns the number of rows in result
- *
- * @param resource result
- * @return integer number_of_rows
- * @access public
- */
- function num_rows($result)
- {
- return @mysql_num_rows($result);
- }
- /**
- * returns the number of affected rows in previous MySQL operation
- *
- * @return integer affected_rows
- * @access public
- */
- function affected_rows()
- {
- return mysql_affected_rows($this->link_id);
- }
- /**
- * returns the primary key generated from the previous INSERT operation
- *
- * @return integer id
- * @access public
- */
- function insert_id()
- {
- return mysql_insert_id($this->link_id);
- }
- /**
- * dies with an error message
- *
- * @param string msg error message
- * @return void;
- * @access private
- */
- function error($msg)
- {
- die("<font face=Verdana size=2><b>Database error</b><br>
- $msg<br><br>
- mysql error: ".mysql_error()."<br>
- mysql error no: ".mysql_errno());
- }
- /**
- * returns the tableprefix
- *
- * @return string tableprefix
- * @access public
- */
- function getPrefix()
- {
- return $this->tableprefix;
- }
- /**
- * print outs the number of sql queries and the executed sql queries.
- *
- * @return void
- * @access public
- */
- function debug()
- {
- echo "<p align='center' class='small'>SQL Queries: ".$this->querycounter."</p>\n";
- echo "<ul class='small'>\n";
- for($i=0;$i<$this->querycounter;$i++) echo "<li>".$this->queries[$i]."</li>\n";
- echo "</ul>\n";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement