Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // direct access not allowed
- if (!defined('ROOT')) die('Direct access not allowed.');
- class Database {
- // debug flag for showing error messages
- public $debug = true;
- private $error = "";
- private $link_id = 0;
- private $query_id = 0;
- public $affected_rows = 0;
- #-#############################################
- # desc: constructor
- function __construct($server=null, $user=null, $pass=null, $database=null)
- {
- // error catching if not passed in
- if($server==null || $user==null || $database==null){
- $this->oops("No mysql info");
- }
- else
- {
- $this->link_id = @mysql_connect($server,$user,$pass);
- if (!$this->link_id){
- //open failed
- $this->oops("Could not connect to server: <b>".DB_SERVER."</b>.");
- }
- if(!@mysql_select_db(DB_DATABASE, $this->link_id))
- {
- //no database
- $this->oops("Could not open database: <b>".DB_DATABASE."</b>.");
- }
- }
- }#-#constructor()
- #-#############################################
- # desc: close the connection
- public function close(){
- if(!@mysql_close($this->link_id)){
- $this->oops("Connection close failed.");
- }
- }#-#close()
- #-#############################################
- # Desc: executes SQL query to an open connection
- # Param: (MySQL query) to execute
- # returns: (query_id) for fetching results etc
- public function query($sql){
- // do query
- $this->query_id = @mysql_query($sql, $this->link_id);
- if (!$this->query_id){
- $this->oops("<b>MySQL Query fail:</b> $sql");
- return 0;
- }
- $this->affected_rows = @mysql_affected_rows($this->link_id);
- return $this->query_id;
- }#-#query()
- #-#############################################
- # desc: fetches and returns results one line at a time
- # param: query_id for mysql run. if none specified, last used
- # return: (array) fetched record(s)
- public function fetch($query_id=-1){
- // retrieve row
- if ($query_id!=-1){
- $this->query_id=$query_id;
- }
- if (isset($this->query_id)){
- $record = @mysql_fetch_assoc($this->query_id);
- }else{
- $this->oops("Invalid query_id: <b>$this->query_id</b>. Records could not be fetched.");
- }
- return $record;
- }#-#fetch()
- #-#############################################
- # desc: frees the resultset
- # param: query_id for mysql run. if none specified, last used
- private function free_result($query_id=-1){
- if ($query_id!=-1){
- $this->query_id=$query_id;
- }
- if($this->query_id!=0 && !@mysql_free_result($this->query_id)){
- $this->oops("Result ID: <b>$this->query_id</b> could not be freed.");
- }
- }#-#free_result()
- #-#############################################
- # desc: throw an error message
- # param: [optional] any custom error to display
- private function oops($msg=''){
- if(!empty($this->link_id)){
- $this->error = mysql_error($this->link_id);
- }
- else{
- $this->error = mysql_error();
- $msg="<b>WARNING:</b> No link_id found. Likely not be connected to database.<br />$msg";
- }
- // if no debug, done here
- if(!$this->debug) return;
- ?>
- <table align="center" border="1" cellspacing="0" style="background:white;color:black;width:80%;">
- <tr><th colspan=2>Database Error</th></tr>
- <tr><td align="right" valign="top">Message:</td><td><?php echo $msg; ?></td></tr>
- <?php if(!empty($this->error)) echo '<tr><td align="right" valign="top" nowrap>MySQL Error:</td><td>'.$this->error.'</td></tr>'; ?>
- <tr><td align="right">Date:</td><td><?php echo date("l, F j, Y \a\\t g:i:s A"); ?></td></tr>
- <?php if(!empty($_SERVER['REQUEST_URI'])) echo '<tr><td align="right">Script:</td><td><a href="'.$_SERVER['REQUEST_URI'].'">'.$_SERVER['REQUEST_URI'].'</a></td></tr>'; ?>
- <?php if(!empty($_SERVER['HTTP_REFERER'])) echo '<tr><td align="right">Referer:</td><td><a href="'.$_SERVER['HTTP_REFERER'].'">'.$_SERVER['HTTP_REFERER'].'</a></td></tr>'; ?>
- </table>
- <?php
- }#-#oops()
- }
- ?>
Add Comment
Please, Sign In to add comment