namespace core;
* 資料庫操作類
* @author chenyuwen
class database
* 鏈結目標物件
* @var string
protected $db_target = null;
* 使用者名稱
* @var string
protected $user_name = null;
* 密碼
* @var string
protected $password = null;
* 引數
* @var array
protected $params = array();
* pdo物件
* @var pdo
protected $pdo = null;
* 資料宣告
* @var \pdostatement
protected $statement = null;
* runtime執行的所有sql
* @var array
public static $queries = array ();
* 最後一次執行的sql
* @var string
public static $last_query = null;
* 設定資料庫連線引數
* @param array $config
* @code php
* $db_target = 'host=127.0.0.1;port=3306;dbname=yuwenc';
* $user_name = 'yuwenc';
* $password = 'yuwenc';
* $params = array (pdo::mysql_attr_init_command => "set names utf8") ;
* $mysql = new \core\mysql($dbtarget, $username, $password, $params);
* @endcode
public function __construct($db_target, $user_name, $password, $params)
$this->db_target = $db_target;
$this->user_name = $user_name;
$this->password = $password;
$this->params = $params;
* 延遲連線資料庫,當需要使用資料庫操作的時候才連線
* @return \pdo
protected function connect()
if (is_null($this->pdo))
try$this->pdo = new \pdo($this->db_target, $this->user_name, $this->password, $this->params);
$this->pdo->setattribute(\pdo::attr_errmode, \pdo::errmode_exception);
catch ( \pdoexception $e )
exit ( "error: failed to connect database: \n" );
return $this->pdo;
* 關閉資料庫連線
public function __destruct()
$this->pdo = null;
* 開始事務
public function begin_transaction()
$this->connect()->begintransaction ();
* 提交事務
* @return
public function commit()
$this->connect()->commit ();
* 回滾事務
public function roll_back()
$this->connect()->rollback ();
* quotes a string for use in a query
* @param mixed $value to quote
* @return string
public function quote($value)
return $this->connect()->quote ( $value );
* 執行乙個sql語句,返回乙個statement 物件
* @param string $sql query to run
* @param array $params the prepared query params
* @return \core\database
public function query($sql, array $params = null)
try$time = microtime ( true );
self::$last_query = $sql;
$this->statement = $this->connect()->prepare ( $sql );
$this->statement->execute ( $params );
self::$queries = array (microtime ( true ) - $time, $sql );
return $this;
catch ( \exception $e )
throw new \exception($e->getmessage()."\nsql:");
* 獲取一筆資料
* @param string $fetch_style
* @param string $cursor_orientation
* @param int $cursor_offset
public function row($fetch_style = \pdo::fetch_assoc)
return $this->statement->fetch ( $fetch_style );
* 獲取一筆資料中的某一列資料
* @param int $column_number
public function column($column_number = 0)
return $this->statement->fetchcolumn ( $column_number );
* 獲取n筆資料
* @param string $fetch_style
public function fetch($fetch_style = \pdo::fetch_assoc)
return $this->statement->fetchall ( $fetch_style );
* 返回受影響的資料行數 (for insert/delete/update)
* @return int
public function row_count()
return $this->statement->rowcount ();
* 返回執行成功新增資料操作時生成的 last_insert_id (for insert)
* @return int
public function last_insert_id()
return $this->pdo->lastinsertid ();
編譯安裝pdo mysql
今天需要使用mysql的pdo,所以就使用了phpize安裝pdo擴充套件 安裝 pdo拓展 進入php源 包ext pdo usr local php bin phpize configure with php config usr local php bin php config make ma...
php 新增pdo mysql擴充套件
幫朋友安裝禪道管理軟體的時候碰到的問題,鬱悶了幾天終於解決了 之前apache php mysql 都是好的 可是安裝禪道的時候 報pdo mysql擴充套件沒有加上 我的解決方法是 安裝mysql mysql server 5.5.15 1.rhel5.x86 64.rpm mysql clien...
增加pdo mysql單獨安裝
原來編譯php的時候,沒有把dpo mysql相關的引數帶上,安裝 完後才發現。再重新編譯有點費時間,所以決定單獨來安裝。先找需要的版本,我用的是穩定的版本。要先看看說明,特別是要注意mysql的php的版本 wget tar xzvf pdo mysql 1.0.2.tgz cd pdo mysq...