Interacting with ODBC Data Sources

ODBC provides a data abstraction layer that is particularly useful for accessing some of Microsoft’s products—such as Access, Excel, MS SQL Server, and others—through a common interface. It’s like the PEAR DB abstraction class we talked about in Chapter 8. In this section we show you how to configure a database for control via ODBC, and how to access an ODBC database from PHP.

Configuring a DSN

As with PEAR DB, you identify an ODBC database with a data source name (DSN). With ODBC, however, you must explicitly create the mapping between a DSN and its database. This section steps through configuring the built-in Excel ODBC driver, but the process is similar for Access, MySQL, and other databases.

Open the Control Panels folder, and double-click on the ODBC Data Sources icon. The resulting dialog box is the ODBC Data Source Administrator. Select the System DSN tab, click the Add button, and select the driver for your target database. If the driver is not listed, you will need to obtain one from your database vendor. If you’ve installed Microsoft Office products on your computer, you will have all the drivers that you need to use Excel as a primitive database. Figure 15-5 shows the addition of a System DSN for a Microsoft Excel workbook.

Configuring a DSN for a Microsoft Excel spreadsheet located at C:\php\phonelist.xls

Figure 15-5. Configuring a DSN for a Microsoft Excel spreadsheet located at C:\php\phonelist.xls

Press the Configure ...

Get Programming PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.