PHP中的PDO函數庫

來源:互聯網
上載者:User

標籤:format   llb   nsa   base   hand   res   ada   rowset   att   

PDO是一個“資料庫訪問抽象層”,作用是統一各種資料庫的提供者,與mysql和mysqli的函數庫相比,PDO讓跨資料庫的使用更具有親和力;與ADODB和MDB2相比,PDO更高效。

目前而言,實現“資料庫抽象層”任重而道遠,使用PDO這樣的“資料庫訪問抽象層”是一個不錯的選擇。

PDO中包含三個預定義的類

PDO中包含三個預定義的類,它們分別是 PDO、PDOStatement 和 PDOException。

 

一、PDO

PDO->beginTransaction() — 標明復原起始點
PDO->commit() — 標明復原結束點,並執行SQL
PDO->__construct() — 建立一個PDO連結資料庫的執行個體
PDO->errorCode() — 擷取錯誤碼
PDO->errorInfo() — 擷取錯誤的資訊
PDO->exec() — 處理一條SQL語句,並返回所影響的條目數
PDO->getAttribute() — 擷取一個“資料庫連接對象”的屬性
PDO->getAvailableDrivers() — 擷取有效PDO磁碟機名稱
PDO->lastInsertId() — 擷取寫入的最後一條資料的主索引值
PDO->prepare() — 產生一個“查詢對象”
PDO->query() — 處理一條SQL語句,並返回一個“PDOStatement”
PDO->quote() — 為某個SQL中的字串添加引號
PDO->rollBack() — 執行復原
PDO->setAttribute() — 為一個“資料庫連接對象”設定屬性

二、PDOStatement

PDOStatement->bindColumn() — Bind a column to a PHP variable
PDOStatement->bindParam() — Binds a parameter to the specified variable name
PDOStatement->bindValue() — Binds a value to a parameter
PDOStatement->closeCursor() — Closes the cursor, enabling the statement to be executed again.
PDOStatement->columnCount() — Returns the number of columns in the result set
PDOStatement->errorCode() — Fetch the SQLSTATE associated with the last operation on the statement handle
PDOStatement->errorInfo() — Fetch extended error information associated with the last operation on the statement handle
PDOStatement->execute() — Executes a prepared statement
PDOStatement->fetch() — Fetches the next row from a result set
PDOStatement->fetchAll() — Returns an array containing all of the result set rows
PDOStatement->fetchColumn() — Returns a single column from the next row of a result set
PDOStatement->fetchObject() — Fetches the next row and returns it as an object.
PDOStatement->getAttribute() — Retrieve a statement attribute
PDOStatement->getColumnMeta() — Returns metadata for a column in a result set
PDOStatement->nextRowset() — Advances to the next rowset in a multi-rowset statement handle
PDOStatement->rowCount() — Returns the number of rows affected by the last SQL statement
PDOStatement->setAttribute() — Set a statement attribute
PDOStatement->setFetchMode() — Set the default fetch mode for this statement

PDO是一個“資料庫訪問抽象層”,作用是統一各種資料庫的提供者,與mysql和mysqli的函數庫相比,PDO讓跨資料庫的使用更具有親和力;與ADODB和MDB2相比,PDO更高效。目前而言,實現“資料庫抽象層”任重而道遠,使用PDO這樣的“資料庫訪問抽象層”是一個不錯的選擇。

 

三、PDOException

PDO 提供了3中不同的錯誤處理策略。
1. PDO::ERRMODE_SILENT
這是預設使用的模式。PDO會在statement和database對象上設定簡單的錯誤代號,你可以使用PDO->errorCode() 和 PDO->errorInfo() 方法檢查錯誤;如果錯誤是在對statement對象進行調用時導致的,你就可以在那個對象上使用 PDOStatement->errorCode() 或 PDOStatement->errorInfo() 方法取得錯誤資訊。而如果錯誤是在對database對象調用時導致的,你就應該在這個database對象上調用那兩個方法。
2. PDO::ERRMODE_WARNING
作為設定錯誤代號的附加,PDO將會發出一個傳統的E_WARNING資訊。這種設定在除錯和調試時是很有用的,如果你只是想看看發生了什麼問題而不想中斷程式的流程的話。
3. PDO::ERRMODE_EXCEPTION
作為設定錯誤代號的附件,PDO會拋出一個PDOException異常並設定它的屬性來反映錯誤代號和錯誤資訊。這中設定在除錯時也是很有用的,因為他會有效“放大(blow up)”指令碼中的出錯點,非常快速的指向一個你代碼中可能出錯地區。(記住:如果異常導致指令碼中斷,交易處理回自動復原。)
異常模式也是非常有用的,因為你可以使用比以前那種使用傳統的PHP風格的錯誤處理結構更清晰的結構處理錯誤,比使用安靜模式使用更少的代碼及嵌套,也能夠更加明確地檢查每個資料庫訪問的傳回值。
關於PHP中異常的更多資訊請看Exceptions章節
PDO 使用基於SQL-92 SQLSTATE 的錯誤代號字串;特定的PDO驅動應當將自己本身的代號對應到適當的SQLSTATE代號上。PDO->errorCode() 方法只返回單一的SQLSTATE代號。如果你需要關於一個錯誤的更加有針對性的資訊,PDO也提供了一個PDO->errorInfo()方法,它可以返回一個包含了SQLSTATE代號,特定資料庫驅動的錯誤代號和特定資料庫驅動的錯誤說明字串。

PHP中的PDO函數庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.