ODBC-PHP HOWTO
Last updated May 15, 2000
The
following is a HOWTO document for installing PHP libraries with Openlink ODBC
or iODBC as an Apache module on Linux/Unix systems. Feel free to criticize,
suggest modifications, or ask further questions. It is currently maintained by
Andrew Hill of Openlink Software. ahill@openlinksw.com.
Prerequisites
include basic Unix familiarity, such as creating directories and users, using
an editor, etc.
This
HOWTO is intended to assist in connecting php/apache to back end databases via
ODBC in a development environment and should not take the place of thorough
testing before deployment on a production system.
ODBC (Open Database Connectivity) is an operating system
and database independent communication API that allows a client application
(productivity tool, other database, web page, custom application, etc.) to communicate
via standard-based function calls to a back end database without relying on
that vendors proprietary communication protocols.
ODBC connections involve an
application, driver manager, driver, and database. The driver manager under Microsoft Windows platforms is the ODBC
Control Panel. The driver manager registers
a set of ODBC driver connection parameters called a Data Source Name
(DSN). An application looks to the driver
manager for a DSN, and then passes the connection parameters specified in the
DSN to the appropriate driver, which makes the connection.
A driver manager also makes ODBC
API call translations between different versions of the ODBC specification
where necessary. Thus, its possible to
use a driver that is ODBC 2.5 compatible and make ODBC 3 calls against it, as
long as the driver manager is ODBC 3 compliant, and can make the necessary
translations.
Under non-Windows platforms,
this driver manager will be supplied by third parties or sometimes bypassed
altogether. Openlink Software maintains
an open-source driver manager and SDK called iODBC, which is available at www.iodbc.org. As you will note above, the driver manager is not a driver, and
does not enable connectivity by itself.
Compiling PHP with iODBC will make your php scripts ODBC-aware and
will allow you to use any ODBC driver with them, but does not provide you with
a driver directly. The SDK is also
useful when building ODBC-compliant applications.
Openlink uses a similar iODBC
library in its commercial drivers, but one that is optimized for our drivers
and may be out-of-synch with the iODBC distribution version. This
means that you should choose to compile PHP with either Openlink, or iODBC, but
you do not need to do both.
Openlink has a 2-tier driver architecture
under linux/unix platforms. This includes
a driver and driver manager on the client side, and a request broker and
database agent on the server side. The
request broker listens for connection calls and initiates a connection between
the database agent and the database.
Openlinks database agent speaks directly to the CLI (Call Level
Interface) of the database, bypassing the vendor specific communication layer,
avoiding the addition of an additional
communication layer. This combined with
a thorough and efficient implementation of the ODBC specification, produces
very fast drivers with robust ODBC support.
Often SQL syntax that is not explicitly supported by database vendors is
enforced onto the database by Openlinks drivers. Full ANSI SQL92 support is enabled, allowing such things as
natural joins, is bi-directional scrollable cursor support, etc.
N.B. Under linux/unix platforms, the driver manager and associated
library files are located by setting environment variables. These must be set before compiling apache
and php with ODBC support.
Download
the Openlink-maintained iODBC source code or RPM package from http://www.iodbc.org. Compile source or install via
RPM in your system:
To
compile: (also refer to the README and INSTALL file to review changes)
To
install via RPM
Repeat
the above instructions for the SDK and install script available at the same
location. The odbcsdk will contain an /odbcsdk/doc/odbc.doc file that includes
instructions for setting up odbc data sources. After setting up the SDK, you
can test ODBC using the odbctest binary in the /odbcsdk/examples/ directory
(see odbctest comments in the next section).
Openlink
Multi-tier drivers involve installation on both the client and server sides of
your architecture. Proceed to http://www.openlinksw.com and follow the "Product
Availability and Download" link. Select Multi-Tier drivers, and answer the
questions about server/client/database versions. For testing purposes this
HOWTO will assume the unix/linux machine is both your client and server.
The
Openlink odbcsdk contains many tools and utilities, as well as source code, and
is available at the same location.
5.
PHP/Apache Quick
Install Instructions
(copied from the PHP installation section of the manual)
Please choose to use either with-openlink or with-iodbc.
Download
most recent source of apache from http://www.apache.org/dist/
Download
the most recent version of PHP from www.php.net.
Check
an output of your env command for the values of LD_LIBRARY_PATH. If not applied, do so, either by running
your profile (section 4g above) or by manually setting it: export
LD_LIBRARY_PATH=/path/to/iodbc/libraries/directory
6.
Sample
Connection
This
code can be saved at test.php3 in your /www/htdocs/ and you can examine it via
a browser pointing to http://domain_name/test.php3.
Notice
the putenvs. These variables include ones
necessary for an Openlink driver connection.
Third-party drivers with iODBC may require different environment variables. I usually put all the necessary variables in
an putenv.inc file, and require it at the beginning of each script that wants
ODBC connectivity. Not a huge deal for
a small site with 3 putenvs on each page, but in practice you could have many
pages with many environment variables on each one. Its cleaner to only be able to make mistakes in one place. This also allows you to apply different sets of envs to different scripts,
interchanging values with great flexibility.
<?putenv("LD_LIBRARY_PATH=/usr/local/openlink/odbcsdk/lib");putenv("ODBCINSTINI=/usr/local/openlink/odbcinst.ini");putenv("ODBCINI=/usr/local/openlink/odbc.ini");$dsn="DSN=OracleLocal"; // this is a valid DSN set up in the above odbc.ini file, tested in odbctest$user="scott"; //default user for the demo Oracle database$password="tiger"; //default password for demo Oracle database $sql="SELECT * FROM EMP"; // directly execute mode if ($conn_id=odbc_connect("$dsn","","")){ echo "connected to DSN: $dsn"; if($result=odbc_do($conn_id, $sql)) { echo "executing '$sql'"; echo "Results: "; odbc_result_all($result); echo "freeing result"; odbc_free_result($result); }else{ echo "can not execute '$sql' "; } echo "closing connection $conn_id"; odbc_close($conn_id);}else{ echo "can not connect to DSN: $dsn ";}?>