class mysql_Class_mysql.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 "
Error
";

echo mysql_error()."
";

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);

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值