mysql model class php_封装好的MySQL.class.php类

private $port; //端口号

private $user; //用户名

private $pass; //密码

private $dbname; //数据库名

private $charset;//字符集

private $link; //保存连接数据库资源

private static $instance = null; //单例模式中的$MySQLDB对象

/**

*@param array $config 配置文件*/

private function __construct ($config) {//初始化属性的值

$this->init($config);//连接数据库

$this->my_connect();//设置字符集

$this->my_charset();//选择数据库

$this->my_dbname();

}/**

* 只能new一次,单例模式

*@param array $config 传递给构造方法的数组参数*/

public static function getInstacne($config) {if (self::$instance == null) {

self::$instance = new self($config);

}return self::$instance;

}/***

*私有化克隆魔术方法*/

private function_clone() {}/**

*初始化

*@param array $config 传递给初始化方法的数组参数*/

private function init($config) {$this->host = isset($config['host'])?$config['host']:'localhost';$this->port = isset($config['port'])?$config['port']:'3306';$this->user = isset($config['user'])?$config['user']:'root';$this->pass = isset($config['pass'])?$config['pass']:'';$this->dbname = isset($config['dbname'])?$config['dbname']:'';$this->charset = isset($config['charset'])?$config['charset']:'utf8';

}/**

*错误测试方法

*@param string $sql sql语句

*直接用途:用来增删改*/

public function my_query($sql) {if ($result = mysql_query($sql)) {return $result;

}else{echo "执行sql语句失败;
";echo "错误代码:",mysql_errno(),"
";echo "错误信息:",mysql_error(),'
';return false;

}

}/**

*连接数据库*/

private functionmy_connect() {if ($link = mysql_connect("$this->host:$this->port",$this->user,$this->pass)) {$this->link = $link;

}else{echo "连接数据库失败:
";echo "错误代码:",mysql_errno(),"
";echo "错误信息:",mysql_error(),'
';return false;

}

}/**

*设置字符集*/

private functionmy_charset() {$sql = "set names $this->charset";$this->my_query($sql);

}/**

*选择数据库*/

private functionmy_dbname() {$sql = "use $this->dbname";return $this->my_query($sql);

}/**

*返回多行多列的结果集,二维数组

*@param string sql语句

*@return mixed(array|false) *执行成功返回数组,失败返回false*/

public function fetchAll($sql) {if($result = $this->my_query($sql)) {//返回资源结果集,遍历

$rows[] = '';while ($row = mysql_fetch_assoc($result)) {$rows[] = $row;

}//结果集使用完毕,主动释放

mysql_free_result($result);//返回二维数组

return $rows;

}else{return false;

}

}/**

* 返回一行多列的结果集,一维数组

*@param string 一条sql语句

*@return mixed(array|false) 执行成功返回数组,失败返回false*/

public function fetchRow($sql) {if($result = $this->my_query($sql)) {//返回资源结果集,遍历

$row = mysql_fetch_assoc($result);//结果集使用完毕,主动释放

mysql_free_result($result);//返回一维数组

return $row;

}else{return false;

}

}/**

*返回一行一列的结果集,单一值

*@param string 一条sql语句

*@return mixed(array|false) 执行成功返回数组,失败返回false*/

public function fetchColumn($sql) {if($result = $this->my_query($sql)) {//返回资源结果集,提取

$row = mysql_fetch_row($result);//结果集使用完毕,主动释放

mysql_free_result($result);//返回单一值

return isset($row[0])?$row[0]:false;

}else{return false;

}

}/**

*析构方法*/

public function__destruct() {

@mysql_close($this->link);

}/**

*__sleep 序列化对象的时候触发执行*/

public function__sleep() {return array('host','port','user','pass','charset','dbname') ;

}/**

*__wakeup 反序列化的时候触发执行

*初始化操作*/

public function__wakeup() {//初始化操作

// 连接数据库

$this->my_connect();//设置字符集

$this->my_charset();//选择数据库

$this->my_dbname();

}/**

*__set 为一个不可访问的属性赋值的时候自动触发

*@param string $name 属性名

*@param mixed $value 属性值*/

public function __set($name,$value) {$allow_set = array('host','port','user','pass','dbname','charset');if(in_array($name,$allow_set)) {//当前属性可以被赋值

$this->$name = $value;

}

}/**

*__get *获得一个不可访问的属性的值的时候自动触发

*@param string $name 属性名*/

public function __get($name) {$allow_get = array('host','port','user','pass','dbname','charset');if (in_array($name,$allow_get)) {return $this->$name;

}

}/**

*__call 访问一个不可访问的对象方法的时候触发

*@param string $name

*@param array $argument 参数列表*/

public function __call($name, $argument) {echo "对不起,您访问的".$name."()方法不存在!
";

}/**

*__callstatic 访问一个不可访问的类方法(静态方法)的时候触发

*@param string $name

*@param array $argument 参数列表*/

public static function __callStatic($name, $argument) {echo "对不起,您访问的".$name."()静态方法不存在!
";

}/**

*调用初始化方法

*@param array $config 配置文件数组*/

public function get_init($config) {$this->init($config);

}/** 调用连接数据库方法*/

public functionget_my_connect() {$this->my_connect();

}/** 调用设置字符集方法*/

public functionget_my_charset() {$this->my_charset();

}/** 调用选择数据库方法*/

public functionget_my_dbname() {$this->my_dbname();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值