Accessing Databases with ADODB - Connecting, Queries and Quoting
(Page 2 of 12 )
The Connect() method calls the appropriate underlying PHP functions to connect to the appropriate database. ADODB also has a PConnect() method to establish a persistent connection to the database. This is supported for Oracle, MySQL, PostgreSQL, Microsoft SQL Server, and ODBC. The tests/testdatabases.inc.php file in the ADODB distribution includes lots of example syntax for Connect() and PConnect() with the different kinds of databases that ADODB supports. Table 2-1 lists the driver strings that can be passed to ADONewConnection() and the databases to which they correspond. Because some databases have multiple driver string possibilities, check the online ADODB manual at http://php.weblogs.com/adodb_manual for the latest details on a particular driver string.
Driver String | Database |
| ado | A generic ADO database |
access | Microsoft Access |
ado_access | Microsoft Access via ADO |
vfp | Visual FoxPro |
db2 | DB2 |
fbsql | FrontBase |
ibase | InterBase 6 or earlier |
firebird | InterBase (Firebird version) |
borland_ibase | InterBase 6.5 or later (Borland version) |
informix72 | Informix 7.2 or earlier |
informix | Informix |
mysql | MySQL (without transaction support) |
mysqlt | MySQL (with transaction support) |
| maxsql | Same as mysqlt |
| oci8 | Oracle 8 and 9 |
| oci805 | Oracle 8.0.5 |
| oci8po | Oracle 8 and 9* |
| odbc_oracle | Oracle via ODBC |
| odbc | An ODBC DSN |
| postgres7 | PostgreSQL 7 |
| postgres64 | PostgreSQL 6.4 and earlier |
| postgres | Same as postgres7 |
| sapdb | SAPDB via ODBC |
| sqlanywhere | SQLAnywhere via ODBC |
| sqlite | SQLite (only available with PHP 5) |
| mssql | Microsoft SQL Server 7 or later |
| mssqlpo | Microsoft SQL Server 7 or later** |
| ado_mssql | Microsoft SQL Server via ADO |
| odbc_mssql | Microsft SQL Server via ODBC |
| sybase | Sybase |
Table 2-1. ADODB Driver Strings and Databases
Table notes:
* Uses ? for bind variables in Prepare() and lowercase for field names
** Converts the || concatenation operator to +
Queries and Quoting
The Execute() method is the central way in ADODB to send a query to the database server. In its simplest form, as used previously, you pass Execute() an SQL query, and it returns a record set that holds the results. You can also use placeholders with Execute(). Any question marks in your query are replaced with values from an array passed to Execute() as a second argument. The question marks are placeholders for the values that come from the array. This technique is useful if you need to run similar queries repeatedly:
$sql = 'SELECT * FROM ice_cream WHERE flavor LIKE ?';
// Get information about Chocolate
$rs = $conn->execute($sql,array('Chocolate'));
// Get information about Vanilla
$rs = $conn->execute($sql,array('Vanilla'));
Placeholders also make quoting easier. String values substituted for placeholders are surrounded by single quotes and have single quotes in them escaped in the appropriate manner for the database you’re using. If you’re not using placeholders, you can quote values with the qstr() method:
$flavor = $conn->qstr($_REQUEST['flavor']);
$rs = $conn->execute("SELECT * FROM ice_cream WHERE flavor LIKE $flavor");
This is from Essential PHP Tools, by David Sklar (Apress, ISBN 1590592808). Check it out at your favorite bookstore today. Buy this book now. |
Next: Introducing Record Sets >>
More Database Articles
More By Apress Publishing