<?php
# /**
# * @package Class_mysql.php
# * @version 1.0
# */
Class mysql{
private $server;
private $username;
private $password;
private $database;
private $coding;
private $show_error;
private $pconn;
function __construct($server,$username,$password,$database,$coding,$show_error=false,$pconn=false){
$this->server = $server;
$this->username = $username;
$this->password = $password;
$this->database = $database;
$this->coding = $coding;
$this->show_error = $show_error;
$this->pconn = $pconn;
if(!$this->show_error){error_reporting(0);} //屏蔽所有錯誤
$this->connect();
}
function connect(){ //連接
if($this->pconn){
$this->conn = mysql_pconnect($this->server,$this->username,$this->password) or die($this->error());
}else{
$this->conn = mysql_connect($this->server,$this->username,$this->password) or die($this->error());
}
mysql_select_db($this->database,$this->conn) or die ($this->error());
mysql_query("SET NAMES $this->coding");
}
function selse($table,$where){ //查詢
$this->sql = "SELECT * FROM `$table` $where";
return $this->query($this->sql);
}
function fetch($sql){
$this->result = mysql_fetch_array($sql);
return $this->result;
}
function insert($table,$field,$value){ //插入
$this->sql = "INSERT INTO `$this->database`.`$table` ($field) VALUES($value);";
return $this->query($this->sql);
}
function update($table,$field,$value,$where){ //更新
$this->sql = "UPDATE `$this->database`.`$table` SET `$field` = '$value' $where;";
return $this->query($this->sql);
}
function delete($table,$where){ //刪除
$this->sql = "DELETE FROM `$this->database`.`$table` $where;";
return $this->query($this->sql);
}
function query($sql){ //發送SQL語句
$this->query = mysql_query($this->sql,$this->conn)or die($this->error());
return $this->query;
}
function num($table) { //查詢數據總數
$this->query = $this->query($this->selse($table,''));
$this->num = mysql_num_rows($this->query);
return $this->num ;
}
function error($value=''){ //自定義錯誤
if($this->show_error){
echo "<br>Error<br />";
echo mysql_error()."<br />";
echo $value;
}
}
function check($sql) { //附加功能 - 防注入
$check = eregi('select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql); //過濾危險語句
if($check){
echo "孩子~放下屠刀立地成佛!老衲看好你喲~";
exit();
}else{
return htmlspecialchars($sql,ENT_QUOTES); //格式化HTML
}
}
function __destruct(){ //析構函數,自動關閉數據庫,垃圾回收機制
if(!empty($this->result)){
mysql_free_result($this->result)or die($this->error('為節省系統資源數據庫已被程序自動關閉,請不要重復連接數據庫,或者將連接模式改為永久連接'));
}
mysql_close($this->conn);
}
}
?>