為了讓自己的數據類能夠做到最大化的重用,就寫個能夠重用的PDO操作MySql的類,由于pdo可以連接現在流行的各種數據庫,所以單獨的寫個配置類類來完成不同數據庫DSN的配置,PDO操作MYSQL類代碼如下:
- <?php
- /**
- * 類標準說明 PDO連接數據庫的配置類
- * 類名: ConfigDataBase
- * 功能說明: 為了讓代碼重用,利用此類可以動態的連接各種數據庫
- * 參數說明: $_dbms = "mysql教程"; //數據庫類型
- * $_host = '127.0.0.1'; //數據庫ip地址
- * $_port = '3306'; //數據庫端口
- * $_username = 'root'; //數據庫用戶名
- * $_password = 'liujijun'; //密碼
- * $_dbname = 'zendf'; //數據庫名 默認為zenf
- * $_charset = 'utf-8'; //數據庫字符編碼
- * $_dsn;// //data soruce name 數據源
- *
- *
- * 類屬性說明:
- * 類方法說明:
- * 返回值: 不同函數返回不同的值
- * 備注說明:
- * 作者: 劉紀君
- * 最后一次修改時間: 2011下午02:01:39
- *
- */
- class ConfigDataBase {
- protected static $_dbms = "mysql"; //數據庫類型
- protected static $_host = '127.0.0.1'; //數據庫ip地址
- protected static $_port = '3306'; //數據庫端口
- protected static $_username = 'root'; //數據庫用戶名
- protected static $_password = 'liujijun'; //密碼
- protected static $_dbname = 'zendf'; //數據庫名 默認為zenf
- protected static $_charset = 'utf-8'; //數據庫字符編碼
- protected static $_dsn;// //data soruce name 數據源
- /**
- *@return 返回數據源名
- */
- public static function getDsn() {
- //將變量的值組合成 mysql:host=localhost;port =3306;dbname=test',$login,$passwd的形式
- if (!isset(self::$_dsn)){
- self::$_dsn = self::$_dbms.':host = '.self::$_host.';prot = '.
- self::$_port . ';dbname = ' . self::$_dbname.','.
- self::$_username . ','.self::$_password;
- if (strlen(self::$_charset) > 0){
- self::$_dsn = self::$_dsn . ';charset = ' . self::$_charset;
- }
- }
- return self::$_dsn;//返回數據源名
- }
- /**
- * 功能:設置$dbms
- * @param $dbms
- */
- public static function setDbms($dbms){
- if (isset($dbms) &&(strlen($dbms) > 0 )){
- self::$_dbms = trim($dbms);
- }
- }
- /**
- *
- * @param $host //數據庫地址
- */
- public static function setHost($host){
- if (isset($host) &&(strlen($host) > 0 )){
- self::$_host = trim($host);
- }
- }
- /**
- *
- * @param $host 端口號
- */
- public static function setPort($port){
- if (isset($port) &&(strlen($port) > 0 )){
- self::$_post = trim($port);
- }//開源代碼Vevb.com
- }
- /**
- *
- * @param $passwd 密碼
- */
- public static function setPasswd($passwd){
- if (isset($passwd) &&(strlen($passwd) > 0 )){
- self::$_password = trim($passwd);
- }
- }
- /**
- *
- * @param $username 用戶名
- */
- public static function setUsernName($username){
- if (isset($username) &&(strlen($username) > 0 )){
- self::$_username = trim($username);
- }
- }
- /**
- *
- * @param $dbname 數據庫名
- */
- public static function setDbName($dbname){
- if (isset($dbname) &&(strlen($dbname) > 0 )){
- self::$_dbname = trim($dbname);
- }
- }
- /**
- *
- * @param $charset 數據庫編碼
- */
- public static function setCharset($charset){
- if (isset($charset) &&(strlen($charset) > 0 )){
- self::$_charset = trim($charset);
- }
- }
- }
- 下面是對數據庫的操作:
- <?php
- require_once 'ConfigDataBase.php';
- header("Content-Type: text/html; charset=utf-8");//設置編碼
- /**
- * 類標準說明
- * 類名: PdoMysql
- * 功能說明: 對數據庫進行各種操作
- * 參數說明:
- * 類屬性說明:
- * 類方法說明:
- * 返回值:
- * 備注說明:
- * 作者: 劉紀君
- * 最后一次修改時間: 2011上午10:45:36
- *
- */
- class PdoMysqlOperater{
- /**
- * @return 返回連接數據庫的句柄
- */
- public function getConnection(){
- $connection = NULL;
- try {
- $connection = new PDO(ConfigDataBase::getDsn());
- echo 'Success';
- } catch (PDOException $e) {
- print "Error in connection :".$e->getMessage().' '.die();
- }
- return $connection;
- }
- /**
- *
- * @param $connection 連接數據庫的句柄
- */
- public function closeConnection($connection){
- try {
- if ($connection != null) {
- $connection = null;//關閉數據庫連接句柄
- }
- } catch (Exception $e) {
- print 'Close the connectin is error:'.$e->getMessage();
- }
- }
- /**
- * 功能: 向數據庫中增加數據
- * @param $sql sql語句
- */
- public function insertDatabase($sql){
- $affect = false;//失敗返回false
- try {
- $conn = $this->getConnection();
- $conn->exec($sql);
- $affect = true;//插入成功返回true
- $this->closeConnection($conn);//關閉數據庫
- } catch (PDOException $e) {
- print 'Insert error '.$e->getMessage();
- }
- return $affect;//返回值
- }
- /**
- *
- * @param $id 表的主鍵id
- * @param $tableName 表名
- */
- public function deleltById($id,$tableName){
- $affact = false;
- $sql = 'delete from '.trim($tableName).' where id = '.$id;
- try {
- $conn = $this->getConnection();
- $conn->exec($sql);
- $this->closeConnection($conn);
- $affact = true;
- } catch (PDOException $e) {
- print 'Delelte error is '.$e->getMessage();
- }
- return $affact;
- }
- /**
- * 功能: 以and 的形式刪除記錄
- * @param $tableName 表的名稱
- * @param $array 數組表中字段名=其值的方式進行組合
- */
- public function prepareDeleteAnd($tableName,array $array=null){
- $sql = 'delete from '. $tableName . ' where ';
- $count = count($array);//計算數組的長度
- $flag = 0;//設置標記
- foreach ($array as $key => $value){
- $flag++;//讓flag增加一
- $sql .= $key .'='."'".$value."'";
- if ($flag != $count ){//當falg不等于count時,數組還有值,后面增加and,反之不增加
- $sql .= ' and ';
- }
- }
- echo $sql;//測試sql語句的組合
- try {
- $conn = $this->getConnection();//獲取連接
- $conn->prepare($sql);
- $this->closeConnection();
- } catch (PDOException $e) {
- print 'Delete error is '.$e->getMessage();
- }
- }
- /**
- * 功能: 以or 的形式刪除記錄
- * @param $tableName 表的名稱
- * @param $array 數組表中字段名=其值的方式進行組合
- */
- public function prepareDeleteOr($tableName,array $array=null){
- $sql = 'delete from '. $tableName . ' where ';
- $count = count($array);//計算數組的長度
- $flag = 0;//設置標記
- foreach ($array as $key => $value){
- $flag++;//讓flag增加一
- $sql .= $key .'='."'".$value."'";
- if ($flag != $count ){//當falg不等于count時,數組還有值,后面增加and,反之不增加
- $sql .= ' or ';
- }
- }
- echo $sql;//測試sql語句的組合
- try {
- $conn = $this->getConnection();//獲取連接
- $stmt = $conn->prepare($sql);
- $stmt->execute();//執行
- $this->closeConnection();
- } catch (PDOException $e) {
- print 'Delete error is '.$e->getMessage();
- }
- }
- /**
- * 功能: 取得表中所有數據
- * @param $sql sql語句
- */
- public function getAll($sql){
- $result = null;
- try {
- $conn = $this->getConnection();
- $result = $conn->query($sql);
- $this->closeConnection($conn);
- } catch (PDOException $e) {
- print 'GetAll error is '.$e->getMessage();
- }
- }
- /**
- * 功能:更新數據表中的信息
- * @param $table 要更新的表名
- * @param array $updateFiled 要更新的字段
- * @param array $updateConditon 更新需要的條件
- */
- public function updateDataBase($table,array $updateFiled,array $updateConditon ){
- $sql = 'update from ' .$table .' set ';
- //對set字段進行賦值操作
- $count = count($updateFiled);//獲取要修改數組的長度
- $flag = 0;//設置標記為0
- foreach ($updateFiled as $key => $value){
- $flag++;
- $sql .= $key .'='."'".$value."'";
- if ($flag != $count){
- $sql .=',';
- }
- }
- //對where條件進行賦值
- $countUpdateCondition = count($updateConditon);//獲取要修改數組的長度
- $flag = 0;//設置標記為0
- $sql .= ' where ';
- foreach ($updateConditon as $key => $value){
- $flag++;
- $sql .= $key .'='."'".$value."'";
- if ($flag != $countUpdateCondition){
- $sql .=' and ';
- }
- }
- try {
- $conn = $this->getConnection();
- $conn->exec($sql);
- $this->closeConnection($conn);
- } catch (PDOException $e) {
- print 'Update error is :'.$e->getMessage();
- }
- }
- /**
- * 功能: 根據表和提高的查詢條件進行查詢
- * 返回值: 返回結果集
- * @param $table 數據表名
- * @param array $findCondition 查詢條件
- */
- public function findData($table,array $findCondition){
- $sql = 'select from '.$table .' where ';
- $count = count($findCondition);//獲取查詢條件數組的長度
- $flag = 0;//設置標記為0
- foreach ($findCondition as $key => $value){
- $flag++;
- $sql .= $key .'='."'".$value."'";
- if ($flag != $count){
- $sql .=' and ';
- }
- }
- try {
- $conn = $this->getConnection();
- $conn->exec($sql);
- $this->closeConnection($conn);
- } catch (PDOException $e) {
- print 'find error is :'.$e->getMessage();
- }
- }
- }
- //測試
- $db = new PdoMysqlOperater();
- $db->findData('liujijun',array('name'=>'liujijun','name1'=>'liujijun'));
- ?>
新聞熱點
疑難解答