php单例模式应用于mysql数据库操作
2016-04-08 15:48
501 查看
PHP中有很多的数据库操作,使用单例模式进行数据库的操作,可以避免大量的new操作浪费资源;
使用单例模式可以很方便的实现一个类来全局控制某些配置信息;
单例模式的要点有三个:
一是某个类只能有一个实例;
二是它必须自行创建这个实例;
三是它必须自行向整个系统提供这个实例。
下里面来一个数据库操作的单例模式实例:
class db{
public $conn;
public static $sql;
public static $intance = null;
protected $host;
protected $user;
protected $pass;
protected $database;
protected $charset;
public function __construct($host, $user, $pass, $database, $charset){
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->database = $database;
$this->charset = $charset;
$this->conn = mysql_connect($this->host, $this->user, $this->pass);
if(!mysql_select_db($this->database, $this->conn)){
die('mysql connect failed');
}
mysql_query('set name '.$this->charset, $this->conn);
}
public static function getIntance(){
if(is_null(self::$intance)){
self::$intance = new db($this->host, $this->user, $this->pass, $this->database, $this->charset);
}
return self::$intance;
}
/**
*@param string $table
*@param array $condition
*@param array $field
*@return array
*/
public function getRows($table, $condition=array(), $field=array()){
$where = '';
if(!empty($condition)){
foreach ($condition as $k=>$v){
$where .= $k."='".$v."' and";
}
$where = 'where '.$where.' 1=1';
}
$fieldStr = '';
if(!empty($field)){
foreach ($field as $k=>$v){
$fieldStr .= $v.',';
}
$fieldStr = rtrim($fieldStr, ',');
}else{
$fieldStr = "*";
}
self::$sql = "select {$fieldStr} from {$table} {$where}";
$result = mysql_query(self::$sql, $this->conn);
$resultRow = array();
$i = 0;
while($row = mysql_fetch_assoc($result)){
foreach ($row as $k=>$v){
$resultRow[$i][$k] = $v;
}
$i++;
}
return $resultRow;
}
/**
* @param string $table
* @param array $data
* @param array $condition
* @return boolean
*/
public function update($table, $data=array(), $condition=array()){
$where = '';
if(!empty($condition)){
foreach ($condition as $key=>$val){
$where .= $key."='".$val."' and ";
}
$where = 'where '.$where .' 1=1';
}
$updateStr = '';
if(!empty($data)){
foreach ($data as $key=>$val){
$updateStr .= $key." = '".$val."' ,";
}
$updateStr = 'set '.rtrim($updateStr, ',');
}
self::$sql = "update {$table} {$updateStr} {$where}";
return mysql_query(self::$sql, $this->conn);
}
/**
*
* @param string $table
* @param array $data
* @return number|boolean
*/
public function insert($table, $data=array()){
$fields = '';
$value = '';
if(!empty($data)){
foreach ($data as $k=>$v){
$fields .= $k.',';
$value .= "'$v'".',';
}
$fields = rtrim($fields, ',');
$value = rtrim($value, ',');
}else{
die($data.' is not allowed empty!');
}
self::$sql = "insert into {$table} ({$fields}) values({$value})";
if(mysql_query(self::$sql, $this->conn)){
return mysql_insert_id();
}else{
return false;
}
}
/**
*
* @param string $table
* @param array $condition
* @return resource
*/
public function delete($table, $condition=array()){
$where = '';
if(!empty($condition)){
foreach ($condition as $k=>$v){
$where .= $k." = '".$v."', ";
}
$where = " where ".$where." 1=1";
}
self::$sql = "delete from {$table} {$where}";
return mysql_query(self::$sql, $this->conn);
}
}
使用单例模式可以很方便的实现一个类来全局控制某些配置信息;
单例模式的要点有三个:
一是某个类只能有一个实例;
二是它必须自行创建这个实例;
三是它必须自行向整个系统提供这个实例。
下里面来一个数据库操作的单例模式实例:
class db{
public $conn;
public static $sql;
public static $intance = null;
protected $host;
protected $user;
protected $pass;
protected $database;
protected $charset;
public function __construct($host, $user, $pass, $database, $charset){
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->database = $database;
$this->charset = $charset;
$this->conn = mysql_connect($this->host, $this->user, $this->pass);
if(!mysql_select_db($this->database, $this->conn)){
die('mysql connect failed');
}
mysql_query('set name '.$this->charset, $this->conn);
}
public static function getIntance(){
if(is_null(self::$intance)){
self::$intance = new db($this->host, $this->user, $this->pass, $this->database, $this->charset);
}
return self::$intance;
}
/**
*@param string $table
*@param array $condition
*@param array $field
*@return array
*/
public function getRows($table, $condition=array(), $field=array()){
$where = '';
if(!empty($condition)){
foreach ($condition as $k=>$v){
$where .= $k."='".$v."' and";
}
$where = 'where '.$where.' 1=1';
}
$fieldStr = '';
if(!empty($field)){
foreach ($field as $k=>$v){
$fieldStr .= $v.',';
}
$fieldStr = rtrim($fieldStr, ',');
}else{
$fieldStr = "*";
}
self::$sql = "select {$fieldStr} from {$table} {$where}";
$result = mysql_query(self::$sql, $this->conn);
$resultRow = array();
$i = 0;
while($row = mysql_fetch_assoc($result)){
foreach ($row as $k=>$v){
$resultRow[$i][$k] = $v;
}
$i++;
}
return $resultRow;
}
/**
* @param string $table
* @param array $data
* @param array $condition
* @return boolean
*/
public function update($table, $data=array(), $condition=array()){
$where = '';
if(!empty($condition)){
foreach ($condition as $key=>$val){
$where .= $key."='".$val."' and ";
}
$where = 'where '.$where .' 1=1';
}
$updateStr = '';
if(!empty($data)){
foreach ($data as $key=>$val){
$updateStr .= $key." = '".$val."' ,";
}
$updateStr = 'set '.rtrim($updateStr, ',');
}
self::$sql = "update {$table} {$updateStr} {$where}";
return mysql_query(self::$sql, $this->conn);
}
/**
*
* @param string $table
* @param array $data
* @return number|boolean
*/
public function insert($table, $data=array()){
$fields = '';
$value = '';
if(!empty($data)){
foreach ($data as $k=>$v){
$fields .= $k.',';
$value .= "'$v'".',';
}
$fields = rtrim($fields, ',');
$value = rtrim($value, ',');
}else{
die($data.' is not allowed empty!');
}
self::$sql = "insert into {$table} ({$fields}) values({$value})";
if(mysql_query(self::$sql, $this->conn)){
return mysql_insert_id();
}else{
return false;
}
}
/**
*
* @param string $table
* @param array $condition
* @return resource
*/
public function delete($table, $condition=array()){
$where = '';
if(!empty($condition)){
foreach ($condition as $k=>$v){
$where .= $k." = '".$v."', ";
}
$where = " where ".$where." 1=1";
}
self::$sql = "delete from {$table} {$where}";
return mysql_query(self::$sql, $this->conn);
}
}
相关文章推荐
- lsof -Pni4 | grep LISTEN | grep php
- php如何定义数组常量
- 安装php-solr扩展
- dirname(__FILE__) 获取当前php文件的完整路径
- js调用php和php调用js的方法举例
- Sublime text 2/3 [Decode error - output not utf-8] 完美解决方法
- Sublime text 2/3 [Decode error - output not utf-8] 完美解决方法
- php上传不了文件记得修改权限
- php如何判断是手机访问还是电脑访问
- PHP中的单例模式
- ThinkPHP 3.1.2 查询方式的一般使用2
- ThinkPHP 3.1.2 查询方式的一般使用2
- ThinkPHP 3.1.2 查询方式的一般使用2
- 字节流InputStream/OutputStream
- php自动生成html新闻文件
- Yii2 rules验证规则
- laravel框架下cipher设置出错的几种情况
- ubuntu创建FTP服务
- 5.3 Zend_Log_Writer
- 进阶级-命令行使用