Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database {
- var $server;
- var $username;
- var $password;
- var $database;
- // Exceptions
- var $errno = '0';
- var $error = '0';
- // Link/Query Id's
- var $affected_rows = '0';
- var $link_id = '0';
- var $query_id = '0';
- function __construct($server,$username,$password) {
- $this->server = $server;
- $this->username = $username;
- $this->password = $password;
- }
- public function connect() {
- if ($this->link_id = mysql_connect($this->server,$this->username,$this->password))
- return true;
- else
- $this->exception("Could not connect to ".$this->server);
- $this->server = "";
- $this->username = "";
- $this->password = "";
- }
- public function changeDB($database) {
- if (!empty($database)) {
- $this->database = $database;
- }
- if (mysql_select_db($this->database))
- return true;
- else
- $this->exception("Could not switch database to ".$this->database);
- }
- public function close() {
- if(!@mysql_close($this->link_id)){
- $this->oops("Connection close failed.");
- }
- }
- public function escape($string) {
- if(get_magic_quotes_runtime()) $string = stripslashes($string);
- return @mysql_real_escape_string($string,$this->link_id);
- }
- public function query($sql) {
- $this->query_id = @mysql_query($sql,$this->link_id);
- if (!$this->query_id) {
- $this->exception("Could not run query. SQL: ".$sql);
- return false;
- }
- $this->affected_rows = @mysql_affected_rows($this->link_id);
- return $this->query_id;
- }
- function fetch_array($query_id=-1) {
- if ($query_id!=-1) {
- $this->query_id=$query_id;
- }
- if (isset($this->query_id)) {
- $record = @mysql_fetch_object($this->query_id);
- }else{
- $this->exception("Invalid query_id: <b>".$this->query_id."</b>. Records could not be fetched.");
- }
- return $record;
- }
- public function query_update($table,$data,$where = '1') {
- $q = "UPDATE `".$table."` SET ";
- foreach ($data as $key => $value) {
- switch (strtolower($value)) {
- case "null":
- $q .= $key." = NULL,";
- break;
- case "now()":
- $q .= $key." = NOW(),";
- break;
- default:
- $q .= $key." = ".$this->escape($value).",";
- break;
- }
- }
- $q = rtrim($q, ', ') . ' WHERE '.$where.';';
- return $this->query($q);
- }
- public function query_insert($table,$data) {
- $q = "INSERT INTO `".$table."` ";
- $k = "";$v = "";
- foreach($data as $key => $val) {
- $k.="`$key`, ";
- if(strtolower($val)=='null') $v.="NULL, ";
- elseif(strtolower($val)=='now()') $v.="NOW(), ";
- else $v.= "'".$this->escape($val)."', ";
- }
- $q .= "(". rtrim($k, ', ') .") VALUES (". rtrim($v, ', ') .");";
- if($this->query($q)){
- return mysql_insert_id($this->link_id);
- }
- else return false;
- }
- private function exception($msg = '') {
- if($this->link_id>0){
- $this->error=mysql_error($this->link_id);
- $this->errno=mysql_errno($this->link_id);
- }
- else{
- $this->error=mysql_error();
- $this->errno=mysql_errno();
- }
- ?>
- <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(strlen($this->error)>0) 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>
- <tr><td align="right">Script:</td><td><a href="<?php echo @$_SERVER['REQUEST_URI']; ?>"><?php echo @$_SERVER['REQUEST_URI']; ?></a></td></tr>
- <?php if(strlen(@$_SERVER['HTTP_REFERER'])>0) echo '<tr><td align="right">Referer:</td><td><a href="'.@$_SERVER['HTTP_REFERER'].'">'.@$_SERVER['HTTP_REFERER'].'</a></td></tr>'; ?>
- </table>
- <?php
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement