host = $host; $this->user = $user; $this->pass = $pass; $this->db = $db; } /* Connects to the Database Server */ function connect(){ try { $this->linkid = @mysql_connect($this->host,$this->user,$this->pass); if (! $this->linkid) throw new Exception("Server is unavailable, or is undergoing maintenance."); } catch (Exception $e) { die($e->getMessage()); } // catch } // connect() /* Select your Database */ function select(){ try { if (! @mysql_select_db($this->db, $this->linkid)) throw new Exception ("Could not connect to the MySQL Database!"); } catch (Exception $e) { die ($e->getMessage()); } // catch } // select() /* Execute Query */ function query ($query) { try { $this->result = @mysql_query($query, $this->linkid); if(! $this->result) throw new Exception("The database query failed: " . $query); } catch (Exception $e) { echo ($e->getMessage()); } $this->querycount++; return $this->result; } // query () function safe_query() { $args = func_get_args(); $query = array_shift($args); $args = @array_map("mysql_real_escape_string",(is_array($args[0]) ? $args[0] : $args)); $squery = vsprintf($query,$args); return $this->query($squery); } // safe_query() /* Determine the total rows affected by the last query */ function num_rows(){ $count = @mysql_num_rows($this->result); return $count; } // num_rows() /* Return query result row as an object */ function fetch_object() { $row = @mysql_fetch_object($this->result); return $row; } // fetch_object() /* Return query result row as an indexed array */ function fetch_row() { $row = @mysql_fetch_row($this->result); return $row; } // fetch_row() /* Return query results as an associative array */ function fetch_array() { $row = @mysql_fetch_array($this->result); return $row; } // fetch_array /* Return total number of queries executed during the lifetime of this object */ function num_queries() { return $this->querycount; } // num_queries /* Returns the last ID by auto_increment */ function last_id(){ return mysql_insert_id($this->linkid); } // last_id() /* Change charset -- useful when strange characters appearing on query results */ function charset($which = "utf8"){ mysql_query("SET NAMES '" . $which . "'", $this->linkid); } } // mysql class ?>