您的位置:首页 > 编程语言 > PHP开发

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: