æè¿å¨é¡¹ç®ä¸ç¨å°äºpdoï¼ä¹åä¸ç´ç¨çmysqlç±»ï¼æ¥äºæ¥æåï¼åç°åè½å¤§åå°å¼ï¼äºæ¯æç¨æ¥å£å°è£ äºä¸ä¸ªpdoç±»ï¼å®ç°äºä¸mysql çç¸åå®ç°ã
大æ¦ç¨å°äºï¼åä¾æ¨¡åï¼æ¥å£ï¼éææ¹æ³ï¼mvc
第ä¸æ¬¡åå客ï¼ç´æ¥ä¸ä»£ç å§ï¼
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuUWdn52b09CX0xWdhZWZk9CX09Wbl9lcvRXakVGa49CXy9GdpRWZoh3LcRXZu5ibkN3Yuc2bsJmLjlGdhR3cvw1LcpDc0RHaiojIsJye.gif)
1.å ¬å ±æ¥å£ï¼i-DAO.interface.php
<?php
/**
* Created by PhpStorm.
* User: jiangbo
* Date: 2016/1/24
* Time: 1:05
* ä¸mysqlæ¥å£ä¸è´(模åå±è°ç¨ä¸è´)ï¼å©ç¨interface
*/
interface i_DAO{
//è·åä¸åDAOçæ¥å£
public static function getInstance($config = array());
//æ§è¡sqlçæ¹æ³
public function query($sql = '');
//è·åå
¨é¨æ°æ®
public function fetchAll($sql = '');
//è·åä¸è¡æ°æ®
public function fetchRow($sql = '');
//è·åä¸ä¸ªæ°æ®
public function fetchOne($sql = '');
//转ä¹sqlï¼é²æ¢æ³¨å
¥
public function escapeString($str = '');
}
2.mysqlç±»ï¼MySqlDB.class.php
<?php
/**
* Created by PhpStorm.
* User: jiangbo
* Date: 2016/1/19
* Time: 17:27
* åä¾åçmysqlç±»ï¼3ç§1å
¬
*/
class MySqlDB implements i_DAO
{
private $_host;
private $_port;
private $_user;
private $_password;
private $_charset;
private $_dbname;
private $_link;
/**
* MySqlDB constructor.
* @param array $config
*/
private function __construct($config = array())
{
$this->_initServer($config);//åå§åæå¡å¨ä¿¡æ¯
$this->_connectServer();//é¾æ¥æå¡å¨
$this->_setCharset();//设置å符éç¼ç
$this->_selectDB();//éæ©é»è®¤æ°æ®åº
}
private function __clone()
{
echo "ä¸è½å
é该对象", "<br>";
die();
}
private static $_instance;
public static function getInstance($config = array())
{
if (!(static::$_instance instanceof static)) {
static::$_instance = new static($config);
}
return static::$_instance;
}
private function _initServer($config)
{
$this->_host = isset($config['host']) ? $config['host'] : 'localhost';
$this->_port = isset($config['port']) ? $config['port'] : '3306';
$this->_user = isset($config['user']) ? $config['user'] : '';
$this->_password = $config['password'];
$this->_charset = isset($config['charset']) ? $config['charset'] : 'UTF8';
$this->_dbname = isset($config['dbname']) ? $config['dbname'] : 'test';
}
private function _connectServer()
{
$connect_result = @mysql_connect("$this->_host:$this->_port", $this->_user, $this->_password);
if ($connect_result) {
$this->_link = $connect_result;
} else {
echo 'æ°æ®åºè¿æ¥å¤±è´¥ï¼è¯·ç¡®è®¤æå¡å¨ä¿¡æ¯';
die();
}
}
private function _setCharset()
{
$sql = "SET NAMES $this->_charset";
$this->query($sql);
}
private function _selectDB()
{
$sql = "USE `$this->_dbname`";
$this->query($sql);
}
/**
* æ§è¡SQLè¯å¥
* @param string $sql
* @return mixed æ§è¡ç»æãæ¥è¯¢ç±»çSQL(select, show, desc),æåè¿åç»æéèµæºï¼å¤±è´¥è¿åfalseãéæ¥è¯¢ç±»(insert, delete, update)ï¼æåè¿åtrueï¼å¤±è´¥è¿åfalse.
*/
public function query($sql)
{
$query_result = @mysql_query($sql, $this->_link);
if (false == $query_result) {
echo "SQLæ§è¡å¤±è´¥:", "<br>";
echo "é误çSQL:", "<br>", $sql, "<br>";
echo "é误çæ¶æ¯ä¸º:", "<br>", mysql_errno($this->_link), "<br>";
die();
} else {
return $query_result;
}
}
/**
* @param string $sql é常为:select * from ...
* @return array
*/
public function fetchRow($sql)
{
$result = $this->query($sql);
$row = @mysql_fetch_assoc($result);
@mysql_free_result($result);
return $row;
}
/**
* @param string $sql é常为:select count(*) from ...
* @return string å¦æ没æå¼å°±è¿åNULL
*/
public function fetchOne($sql)
{
$result = $this->query($sql);
$row = @mysql_fetch_row($result);
@mysql_free_result($result);
if ($row)
return $row[0];
else
return NULL;
}
/**
* @param string $sql é常为:select * from ... where ..like 'han%'
* @return array
*/
public function fetchAll($sql)
{
$result = $this->query($sql);
$rows = array();
while ($row = @mysql_fetch_assoc($result))
$rows[] = $row;
@mysql_free_result($result);
return $rows;
}
/*
* å
³éå½åæ°æ®åºè¿æ¥, ä¸è¬æ é使ç¨. è¿æ¥ä¼éphpèæ¬ç»æèªå¨å
³é
*/
/*public function close()
{
return @mysql_close($this->_link);
}*/
/**
* é²æ¢sql注å
¥ï¼è½¬ä¹å符串ï¼å¨æ¨¡åä¸ä½¿ç¨
* @param string $str 带转ä¹çå符串
* @return string 带å¼å·å
裹ç转ä¹åçå符串
*/
public function escapeString($str = '')
{
return "'" . mysql_real_escape_string($str, $this->_link) . "'";
}
}
3.pdoç±»ï¼ PDODB.class.php
<?php
/**
* Created by PhpStorm.
* User: jiangbo
* Date: 2016/1/24
* Time: 1:00
* daoå±ä½¿ç¨daoæ©å±å°è£
å®ç°
*/
class PDODB implements i_DAO
{
private $_host;
private $_port;
private $_user;
private $_password;
private $_charset;
private $_dbname;
private $_dsn;
private $_option;
private $_pdo;
/**
* PDODB constructor.
* @param array $config
*/
private function __construct($config = array())
{
$this->_initServer($config);
$this->_newPDO();
}
private function _initServer($config)
{
$this->_host = isset($config['host']) ? $config['host'] : 'localhost';
$this->_port = isset($config['port']) ? $config['port'] : '3306';
$this->_user = isset($config['user']) ? $config['user'] : '';
$this->_password = $config['password'];
$this->_charset = isset($config['charset']) ? $config['charset'] : 'UTF8';
$this->_dbname = isset($config['dbname']) ? $config['dbname'] : 'test';
}
private function _newPDO()
{
//设置åæ°
$this->_setDSN();//设置æ°æ®æºåæ°
$this->_setOption();//设置é项
$this->_getPDO();//å¾å°PDO对象
}
private function _setDSN()
{
$this->_dsn = "mysql:host=$this->_host;port=$this->_port;dbname=$this->_dbname";
}
private function _setOption()
{
$this->_option = array(
PDO::MYSQL_ATTR_INIT_COMMAND => "set names $this->_charset"
);
}
private function _getPDO()
{
$this->_pdo = new PDO($this->_dsn, $this->_user, $this->_password, $this->_option);
}
private function __clone()
{
echo "ä¸è½å
é该对象", "<br>";
die();
}
private static $_instance;
public static function getInstance($config = array())
{
if (!(static::$_instance instanceof static)) {
static::$_instance = new static($config);
}
return static::$_instance;
}
//æ§è¡æ¹æ³ï¼éç¨çåºæ¯
private static $_queryStr = array(
"select",
"show",
"desc"
);
public function query($sql = '')
{
//使ç¨æ£åè¿æ»¤ï¼åå«ä½¿ç¨queryåexec
foreach (static::$_queryStr as $str){
if (preg_match("/^\s*".$str.".*?/i",$sql)){
//æ¥è¯¢ç±» è¿åç»æé对象
$result = $this->_pdo->query($sql);
}else{
//éæ¥è¯¢ç±» è¿åbool
$result = $this->_pdo->exec($sql) !== false;//æå¯è½æ¯0
}
//å¦ææ§è¡å¤±è´¥ï¼æ¥é
if($result === false){
$error_info = $this->errorInfo();
echo "SQLæ§è¡å¤±è´¥:", "<br>";
echo "é误çSQL:", "<br>", $sql, "<br>";
echo "é误çæ¶æ¯ä¸º:", "<br>", $error_info[2], "<br>";
die();
}else{
return $result;
}
break;
}
}
public function fetchAll($sql = '')
{
$result = $this->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
$result->closeCursor();
return $rows;
}
public function fetchRow($sql = '')
{
$result = $this->query($sql);
$row = $result->fetch(PDO::FETCH_ASSOC);
$result->closeCursor();
return $row;
}
public function fetchOne($sql = '')
{
$result = $this->query($sql);
$string = $result->fetchColumn();
$result->closeCursor();
return $string;
}
public function escapeString($str = '')
{
return $this->_pdo->quote($str);
}
}
4.å¨åºç±»modleä¸è°ç¨ï¼Modle.class.php
class Model{
/**
* DAO : data access object
*/
protected $_dao;//åå¨å®ä¾å好çæ°æ®åºå¯¹è±¡
/**
* Model constructor.
*/
public function __construct()
{
$this->_initDAO();//åå§ååºç¡æ¨¡å
}
protected function _initDAO(){
$config = array(
'host' => '***',
'user' => '***',
'password' => '***',
'dbname' => '***'
);
//$this->_dao = MySqlDB::getInstance($config);//è°ç¨mysqldb
$this->_dao = PDODB::getInstance($config);//è°ç¨pdo
}
}
第ä¸æ¬¡åå客ï¼ç 代ç ä¸å®¹æï¼è½¬è½½è¯·æ æåºå¤ï¼è°¢è°¢å¤§å®¶ã