Database operation class
Create a new database connection class MysqlDatabase.class.php
##1, attributes required for database connection
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 下午 2:57 */ class MysqlDatabase{ //數(shù)據(jù)庫連接信息 private $dbConfig=array( 'host'=>'localhost', 'port'=>'3306', 'user'=>'root', 'pwd'=>'root', 'charset'=>'utf8', 'dbname'=>'php', ); //數(shù)據(jù)庫連接資源 private $link; }
2,Initialization properties
<?php private function initAttr($params){ //初始化屬性 $this->dbConfig=array_merge($this->dbConfig,$params); }
3,Connection number database
<?php //獲取數(shù)據(jù)庫連接 private function connectServer(){ $host=$this->dbConfig['host']; $port=$this->dbConfig['port']; $user=$this->dbConfig['user']; $pwd=$this->dbConfig['pwd']; //連接數(shù)據(jù)庫服務(wù)器 if ($link=mysql_connect("$host:$port",$user,$pwd)){ $this->link=$link; }else{ die('數(shù)據(jù)庫連接失敗,請(qǐng)確認(rèn)信息!'.mysql_error()); } }
4, Query the database and display the error message
<?php //查詢數(shù)據(jù)庫顯示錯(cuò)誤信息 function query($sql){ if($result=mysql_query($sql)){ //執(zhí)行成功 return $result; }else{ //執(zhí)行失敗,顯示錯(cuò)誤信息以便于調(diào)試程序 echo 'sql執(zhí)行失敗:<br>'; echo '錯(cuò)誤的sql為:',$sql,'<br>'; echo '錯(cuò)誤的代碼為:',mysql_errno(),'<br>'; echo '錯(cuò)誤的信息為:',mysql_error(),'<br>'; die(); } }
5, Set the character set
<?php //設(shè)定連接字符集 private function setCharset(){ $sql="set names {$this->dbConfig['charset']}"; $this->query($sql); }
6, construction parameters
<?php //構(gòu)造方法 public function __construct($params=array()) { //初始化屬性 $this->initAttr($params); //連接數(shù)數(shù)據(jù)庫 $this->connectServer(); //設(shè)定字符集 $this->setCharset(); }
7, query a single piece of data and return the result set
<?php //查詢單條數(shù)據(jù)并返回結(jié)果集 function fetchRow($sql){ //執(zhí)行query()函數(shù) if($result=query($sql)){ //從結(jié)果集取得依次數(shù)據(jù)即可 $row=mysql_fetch_array($result,MYSQL_ASSOC); return $row; }else{ return false; } }
8, Query all data and return the result set ##
<?php //查詢所有數(shù)據(jù)并返回結(jié)果集 function fetchAll($sql){ //執(zhí)行query()函數(shù) if($result=query($sql)){ //執(zhí)行成功 //遍歷結(jié)果集 $rows=array(); while($row=mysql_fetch_array($result,MYSQL_ASSOC)){ $rows[]=$row; } //釋放結(jié)果集資源 mysql_free_result($result); return $rows; }else{ //執(zhí)行失敗 return false; } }
9, All codes are displayed as a whole:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 下午 2:57 */ class MysqlDatabase{ //數(shù)據(jù)庫連接信息 private $dbConfig=array( 'host'=>'localhost', 'port'=>'3306', 'user'=>'root', 'pwd'=>'root', 'charset'=>'utf8', 'dbname'=>'php', ); //數(shù)據(jù)庫連接資源 private $link; //構(gòu)造方法,私有化可以防止類在外部被實(shí)例化 private function __construct($params=array()) { //初始化屬性 $this->initAttr($params); //連接數(shù)數(shù)據(jù)庫 $this->connectServer(); //設(shè)定字符集 $this->setCharset(); } private function initAttr($params){ //初始化屬性 $this->dbConfig=array_merge($this->dbConfig,$params); } //獲取數(shù)據(jù)庫連接 private function connectServer(){ $host=$this->dbConfig['host']; $port=$this->dbConfig['port']; $user=$this->dbConfig['user']; $pwd=$this->dbConfig['pwd']; //連接數(shù)據(jù)庫服務(wù)器 if ($link=mysql_connect("$host:$port",$user,$pwd)){ $this->link=$link; }else{ die('數(shù)據(jù)庫連接失敗,請(qǐng)確認(rèn)信息!'.mysql_error()); } } //設(shè)定連接字符集 private function setCharset(){ $sql="set names {$this->dbConfig['charset']}"; $this->query($sql); } //查詢數(shù)據(jù)庫顯示錯(cuò)誤信息 function query($sql){ if($result=mysql_query($sql)){ //執(zhí)行成功 return $result; }else{ //執(zhí)行失敗,顯示錯(cuò)誤信息以便于調(diào)試程序 echo 'sql執(zhí)行失敗:<br>'; echo '錯(cuò)誤的sql為:',$sql,'<br>'; echo '錯(cuò)誤的代碼為:',mysql_errno(),'<br>'; echo '錯(cuò)誤的信息為:',mysql_error(),'<br>'; die(); } } //查詢所有數(shù)據(jù)并返回結(jié)果集 function fetchAll($sql){ //執(zhí)行query()函數(shù) if($result=query($sql)){ //執(zhí)行成功 //遍歷結(jié)果集 $rows=array(); while($row=mysql_fetch_array($result,MYSQL_ASSOC)){ $rows[]=$row; } //釋放結(jié)果集資源 mysql_free_result($result); return $rows; }else{ //執(zhí)行失敗 return false; } } //查詢單條數(shù)據(jù)并返回結(jié)果集 function fetchRow($sql){ //執(zhí)行query()函數(shù) if($result=query($sql)){ //從結(jié)果集取得依次數(shù)據(jù)即可 $row=mysql_fetch_array($result,MYSQL_ASSOC); return $row; }else{ return false; } } //限制腳本實(shí)例化多個(gè)對(duì)象 //單例對(duì)象調(diào)用 private static $instance; //接下來私有化構(gòu)造方法 public static function getInstance($params=array()){ //判斷是否沒有實(shí)例化過 if(!self::$instance instanceof self){ //如果未被實(shí)例化 self::$instance=new self($params); } //返回對(duì)象 return self::$instance; } //私有克隆,在外部調(diào)用克隆clone $對(duì)象名,會(huì)調(diào)用clone方法復(fù)制這個(gè)對(duì)象 private function __clone() { // TODO: Implement __clone() method. } }