日期:2014-05-17 浏览次数:20598 次
<?php
class db{?
????????private $_link;
??????????
????????function __construct($dbhost='localhost',$dbuser='root',$dbpassword='',$dbname='test',$charset='utf-8') {?
????????????$this->_link = mysql_connect($dbhost,$dbuser,$dbpassword,true);?
????????????$this->_link or $this->errmsg('无法连接MYSQL服务器!');?
????????????if ($this->version() > '4.1') {?????????????????
????????????????$this->query('set names '.$charset);????????
????????????}?
????????????
????????????mysql_select_db($dbname,$this->_link) or $this->errmsg('无法连接数据库!');?
????????}?
??????????
????????
????????public function query($sql) {??????????????????????????????????????????????????????????????
????????????$result = mysql_query($sql,$this->_link);?
????????????$result or $this->errmsg('执行SQL语句错误!');?
????????????return $result;?
????????}
???????????
????????
????????
????????
????????
????????public function fetch_array($result,$type = MYSQL_ASSOC) {
????????????return mysql_fetch_array($result,$type);????
????????}
???????????
????????
????????public function fetch_object($result) {?
????????????return mysql_fetch_object($result);?
????????}
???????????
????????
????????public function affected_rows() {?
????????????return mysql_affected_rows($this->_link);?
????????}?
??????????
????????
????????public function free_result($result) {?
????????????return mysql_free_result($result);??
????????}?
??????????
????????
????????public function num_rows($result) {?
????????????return mysql_num_rows($result);?
????????}
????????????
????????
????????public function num_fields($result) {?
????????????return mysql_num_fields($result);?
????????}?
??????????
????????
????????public function insert_id() {?
????????????return mysql_insert_id($this->_link);?
????????}
???????????
????????
????????private function errmsg($msg) {?
????????????$message? = '<strong>一个MySQL错误发生!</strong><br />';?
????????????$message .= '<strong>错误号:</strong>'. mysql_errno($this->_link) .'<br />';?
????????????$message .= '<strong>错误描述:</strong>'. $msg . mysql_error($this->_link) .'<br />';?
????????????$message .= '<strong>错误时间:</strong>'. date('Y-m-d H:i:s');?
????????????exit($message);?
????????}
???????????
????????
????????public function link_id() {?
????????????return $this->_link;?
????????}
????????????
????????
????????public function version() {?
????????????return mysql_get_server_info($this->_link);?
????????}?
??????????
????????
????????function getip() {?
????????????if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {?
????????????????$ip =