Switching to PDO has many advantages:
- offers a consistent API to work with a variety of databases
- exposes high-level objects for the programmer to work with database connections
- low-level drivers perform communication and resource handling with the database server
Basic Workflow
The basic workflow for working with a database can be thought of as a 5-step process:- Establish a connection to the database server and select the database you’ll be working with
- Construct a query to send the server
- Send the query
- Iterate over the returned result rows
- Free the resources used by the result and possibly the database connection
<?php // Step 1: Establish a connection $db = new PDO( "mysql:host=localhost;dbname=testdb" , "testusr" , "secretpass" ); // Step 2: Construct a query $query = "SELECT * FROM foo WHERE bar = " . $db ->quote( $zip ); // Step 3: Send the query $result = $db ->query( $query ); // Step 4: Iterate over the results while ( $row = $result ->fetch(PDO::FETCH_ASSOC)) { print_r( $row ); } // Step 5: Free used resources $result ->closeCursor(); $db = null; |
No comments:
Post a Comment