Danh mục

Bài giảng Phát triển ứng dụng nguồn mở: Bài 3.2 - Đoàn Thiện Ngân

Số trang: 58      Loại file: pdf      Dung lượng: 95.61 KB      Lượt xem: 15      Lượt tải: 0    
tailieu_vip

Xem trước 6 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Bài 3.2 trình bày những kiến thức về PHP-PDO. Chương này gồm có những nội dung chính sau: Database Abstraction Layer, PDO (Connection, error handling, executing queries, prepare statement, transaction. Mời các bạn tham khảo.
Nội dung trích xuất từ tài liệu:
Bài giảng Phát triển ứng dụng nguồn mở: Bài 3.2 - Đoàn Thiện Ngân Bài 3.2 PHP-PDO GV: ĐOÀN THIỆN NGÂN Đoàn Thiện Ngân Bài 3.2 – 1/58 Contents • Database Abstraction Layer. • PDO – Connection – Error handling – Executing queries – Prepare statement – Transaction Đoàn Thiện Ngân Bài 3.2 – 2/58 Database Abstraction Layer Decouple the application and data layers Đoàn Thiện Ngân Bài 3.2 – 3/58 Widespread implementations • MDB2: written in PHP and available as a PEAR package. It presently supports FrontBase, InterBase, MySQL, Oracle, PostgreSQL, QuerySim, and SQLite. • JDBC: Java Database Connectivity standard allows Java programs to interact with any database for which a JDBC driver is available. Among others, this includes Microsoft SQL Server, MySQL, Oracle, and PostgreSQL. • ODBC: Open Database Connectivity interface is one of the most widespread abstraction implementations in use today, supported by a wide range of applications and languages, PHP included. ODBC drivers are offered by all mainstream databases, including those referenced in the above JDBC introduction. • Perl DBI: Perl Database Interface module is Perl’s standardized means for communicating with a database, and it was the inspiration behind PHP’s DB package. Đoàn Thiện Ngân Bài 3.2 – 4/58 PHP Data Objects - PDO • PHP Data Objects (PDO) abstraction layer • Officially released with PHP 5.1 • PDO serves as an ideal replacement for the MDB2 PEAR package and similar solutions • PDO is actually much more than just a database abstraction layer: – Coding consistency – Flexibility – Object-oriented features – Performance Đoàn Thiện Ngân Bài 3.2 – 5/58 Coding consistency • Because PHP’s various database extensions are written by a host of different contributors, the coding approaches are quite inconsistent despite the common set of features. • PDO removes this inconsistency by offering a single interface that is uniform no matter the database. Furthermore, the extension is broken into two distinct components: – PDO core contains most of the PHP-specific code, leaving the various drivers to focus solely on the data. – PDO developers took advantage of considerable knowledge and experience while previously building and maintaining the native database extensions, capitalizing upon what was successful and being careful to avoid what was not. Although a few inconsistencies remain, by and large the database features are nicely abstracted. Đoàn Thiện Ngân Bài 3.2 – 6/58 Flexibility • Because PDO loads the desired database driver at run time, there’s no need to reconfigure and recompile PHP every time a different database is used. • For instance, if your database needs suddenly switch from Oracle to MySQL, just load the PDO_MYSQL driver Đoàn Thiện Ngân Bài 3.2 – 7/58 Object-oriented features & Performance • Object-oriented features: PDO takes advantage of PHP 5’s object-oriented features, resulting in a more refined approach to database interaction than many preceding solutions. • Performance: PDO is written in C and compiled into PHP, which, all other things being equal, provides a considerable performance increase over solutions written in PHP. Đoàn Thiện Ngân Bài 3.2 – 8/58 Installing PDO • PDO is enabled by default as of version PHP 5.1; however, the MySQL PDO driver is not. • Using PHP 5.1 or newer on the Windows platform, we need to add references to the PDO and driver extensions within the php.ini file extension=php_pdo.dll extension=php_pdo_mysql.dll Đoàn Thiện Ngân Bài 3.2 – 9/58 PDO’s Database Options PDO supports quite a few databases • MySQL: Accessible via the PDO_MYSQL driver. • PostgreSQL: Accessible via the PDO_PGSQL driver. • Firebird/InterBase 6: PDO_FIREBIRD driver. • IBM DB2: PDO_IBM driver. • Informix: PDO_INFORMIX driver. • Microsoft SQL Server: PDO_DBLIB driver. • ODBC: PDO_ODBC driver. ODBC is not a database but it enables PDO to be used in conjunction with any ODBC-compatible database not found in this list. • Oracle: PDO_OCI driver (versions 8 through 11g). • 4D: Accessible via the PDO_4D driver. • SQLite 3.X: Accessible via the PDO_SQLITE driver. Đoàn Thiện Ngân Bài 3.2 – 10/58 PDO Class • PDO::beginTransaction — Initiates a transaction • PDO::commit — Commits a transaction • PDO::__construct — Creates a PDO instance representing a connection to a database • PDO::errorCode — Fetch the SQLSTATE associated with the last operation on the database handle • PDO::errorInfo — Fetch extended error information associated with the last operation on the database handle • PDO::exec — Execute an SQL statement and return the number of affected rows • PDO::getAttribute — Retrieve a database connection attribute Đoàn Thiện Ngân Bài 3.2 – 11/58 PDO Class • PDO::getAvailableDrivers — Return an array of available PDO drivers • PDO::inTransaction — Checks if inside a transaction • PDO::lastInsertId — Returns the ID of the last inserted row or sequence value • PDO::prepare — Prepares a statement for execution and returns a statement object • PDO::query — Executes an SQL statement, returning a result set as a PDOStatement object • PDO::quote — Quotes a string for use in a query. • PDO::rollBack — Rolls back a transaction • PDO::setAttribute — Set an attribute Đoàn Thiện Ngân Bài 3.2 – 12/58 Connecting to a Database Server • Before interacting with a database using PDO, you’ll need to establish a server connection and select a database. • This is accomplished through PDO’s c ...

Tài liệu được xem nhiều: