05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

• Applications that want to use the Save As option for Excel data should issue a<br />

CREATE TABLE statement for the new table and then do subsequent INSERT operations<br />

into the new table. INSERT statements result in an append to the table. No<br />

other operations can be done on the table until it is closed and reopened the first<br />

time. After the table is closed the first time, no subsequent inserts can be done.<br />

• The Excel ODBC driver does not support DELETE, UPDATE, CREATE INDEX, DROP<br />

INDEX, orALTER TABLE statements. While it is possible to update values, DELETE<br />

statements do not remove a row from a table based on an Excel spreadsheet.<br />

If you can work with these limitations, combining <strong>PHP</strong> with Excel through an ODBC<br />

interface may be acceptable.<br />

Although the primary source of documentation for the Excel ODBC drivers is the<br />

Microsoft Desktop Database Drivers Help file, invoked from the Help buttons under<br />

ODBC Administrator, you can also determine some of the peculiarities of Excel’s<br />

support for ODBC via Excel’s online help. However, it will take a good deal of poking<br />

around to find what you need. Much of the time, you will find yourself searching<br />

for answers through your favorite search engine, or in the annotated help files at<br />

http://www.php.net.<br />

Working with Access<br />

A more sophisticated example of <strong>PHP</strong>’s ODBC support is demonstrated in our next<br />

example. Here we store the phone-list data in an Access database, which has slightly<br />

more robust ODBC support.<br />

We use only four ODBC functions from <strong>PHP</strong>:<br />

$handle = odbc_connect(dsn, user, password [, cursor_type]);<br />

$success = odbc_autocommit(handle, status);<br />

$result = odbc_exec(handle, sql);<br />

$cols = odbc_fetch_into(result [, rownumber, result_array]);<br />

There are strong parallels between ODBC and PEAR DB. First you connect to the<br />

database, then you execute queries and fetch the results. You need to connect only<br />

once within each script, and the connection is automatically closed when the script<br />

ends.<br />

The odbc_autocommit( ) function controls transactions. By default, changes to the<br />

database (UPDATE, DELETE, and INSERT commands) take place as soon as the query is<br />

executed. That’s the effect of autocommitting. Disable autocommits, however, and<br />

changes will be visible to you but will be rolled back if the script ends without a<br />

COMMIT SQL statement being executed.<br />

Example 15-5 shows a script that lets the user enter a new record into the phone<br />

database. The same script handles displaying the form, displaying the confirmation<br />

Interacting with ODBC Data Sources | 371<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!