PDO封裝有關問題

來源:互聯網
上載者:User
PDO封裝問題。
網上傳了一個PDO的Mysql封裝。怎麼看都覺得有點問題啊。小弟以前學JAVA的,剛剛接觸PHP。代碼如下:
/**
* 資料庫PDO操作
*/
class MysqlPdo {
public static $PDOStatement = null;
/**
* 資料庫的串連參數配置
* @var array
* @access public
*/
public static $config = array ();
/**
* 是否使用永久串連
* @var bool
* @access public
*/
public static $pconnect = false;
/**
* 錯誤資訊
* @var string
* @access public
*/
public static $error = '';
/**
* 單件模式,儲存Pdo類唯一執行個體,資料庫的串連資源
* @var object
* @access public
*/
protected static $link;
/**
* 是否已經串連資料庫
* @var bool
* @access public
*/
public static $connected = false;
/**
* 資料庫版本
* @var string
* @access public
*/
public static $dbVersion = null;
/**
* 當前SQL語句
* @var string
* @access public
*/
public static $queryStr = '';
/**
* 最後插入記錄的ID
* @var integer
* @access public
*/
public static $lastInsertId = null;
/**
* 返回影響記錄數
* @var integer
* @access public
*/
public static $numRows = 0;
// 事務指令數
public static $transTimes = 0;
/**
* 建構函式,
* @param $dbconfig 資料庫連接相關資訊,array('ServerName', 'UserName', 'Password', 'DefaultDb', 'DB_Port', 'DB_TYPE')
*/
public function __construct($dbConfig = '') {
if (! class_exists ( 'PDO' ))
throw_exception ( "不支援:PDO" );

//若沒有傳輸任何參數,則使用預設的資料定義
if (! is_array ( $dbConfig )) {
$dbConfig = array ('hostname' => DB_HOST, 'username' => DB_USER, 'password' => DB_PWD, 'database' => DB_NAME, 'hostport' => DB_PORT, 'dbms' => DB_TYPE, 'dsn' => DB_TYPE . ":host=" . DB_HOST . ";dbname=" . DB_NAME );
}
if (empty ( $dbConfig ['hostname'] ))
throw_exception ( "沒有定義資料庫配置" );
self::$config = $dbConfig; //將傳入的配置參數,傳給Static變數。
if (empty ( self::$config ['params'] )) //???????????????????
self::$config ['params'] = array ();
/*************************************華麗分隔線*******************************************/
if (! isset ( self::$link )) {
$configs = self::$config; //複製一份配置資訊。
if (self::$pconnect) {
$configs ['params'] [constant ( 'PDO::ATTR_PERSISTENT' )] = true;
}
try {
self::$link = new PDO ( $configs ['dsn'], $configs ['username'], $configs ['password'], $configs ['params'] );
} catch ( PDOException $e ) {
throw_exception ( $e->getMessage () );

//exit('串連失敗:'.$e->getMessage());
}
if (! self::$link) {
throw_exception ( 'PDO CONNECT ERROR' );
return false;
}
self::$link->exec ( 'SET NAMES ' . DB_CHARSET );
self::$dbVersion = self::$link->getAttribute ( constant ( "PDO::ATTR_SERVER_INFO" ) );
// 標記串連成功
self::$connected = true;
// 登出資料庫連接配置資訊
unset ( $configs );
}
return self::$link;
}
/**
* 釋放查詢結果
* @access function
*/
static function free() {
self::$PDOStatement = null;
}
/*********************************************************************************************************/
/* 資料庫操作 */
/*********************************************************************************************************/
/**
* 獲得所有的查詢資料
* @access function
* @return array
*/
static function getAll($sql = null) {
self::query ( $sql );
//返回資料集
$result = self::$PDOStatement->fetchAll ( constant ( 'PDO::FETCH_ASSOC' ) );
return $result;
}
/**
* 獲得一條查詢結果
* @access function
* @param string $sql SQL指令
* @param integer $seek 指標位置
* @return array
*/
static function getRow($sql = null) {
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.