How to connect PHP with Sybase ASE database server
Published - 10 Oct 2009
Last Updated - 10 Oct 2009
Author - Sumedh
Note - This tutorial assumes that you know how to compile and install a program from its source.
I searched the whole Internet for a simple PHP, Sybase ASE database connectivity tutorial but most of the existing tutorials are either old or they dont work. After a lot of trial and error I finally figured out how to do it myself.
There are two techniques to connect from PHP to a Sybase ASE/MS SQL server.
1) TDS (Tabular Data Stream) protocol, which is the application-level protocol used by Sybase and Microsoft databases.
2) ODBC (Open Database Connectivity). Database independent communication API which allows applications to communicate, via standard-based function calls to a back end database management system.
There are two implementations of the TDS protocol. The first implementation is Sybase Open Client Library, which Sybase released as publicly available in binary form. (http://www.sybase.com/linux/ase). The second is FreeTDS, an opensource project to document and implement the Tabular Data Stream protocol. This tutorial will have instructions for both. I know all this sounds complicated but don't worry the next steps are easy.
Which is the best library of them all, to be honest I have no idea, it depends on your application. To know more click here.
What do I need
Apache Web Server, PHP, FreeTDS, OR Sybase ASE, Sample Php file
Note - I am using Apache 2.2.13, PHP 5.2.6 on GNU/Linux.
Configuring Apache
Download and extract apache
./configure --prefix=/usr/u/sumedh/web4/apache --enable-so --enable-rewrite
make
make install
make
make install
Note - You normally need to be root to 'make install', unless you used the --prefix option during configuration to install into your own directory.
This tutorial is divded into two parts, method 1 uses PHP and FreeTDS, method 2 uses PHP and Sybase ASE
Method 1 - Using FreeTDS, Skip this part if you want to use ASE
Configuring FreeTDS
Download and extract FreeTDS and run the following commands.
./configure --prefix=/usr/u/sumedh/web4/freetds
make
make install
make
make install
Configuring PHP
Download and extract PHP
./configure --prefix=/usr/u/sumedh/web4/php --with-apxs2=/usr/u/sumedh/web4/apache/bin/apxs --with-sybase-ct=/usr/u/sumedh/web4/freetds
Before we run the make command we have to edit the Makefile. Php tries to access -lsybtcl which is used by both Sybase and TCL. Remove the following entries from the Makefile "-lsybtcl", "-lintl", "-lcomn" and "-lcs".
make
make install
make install
Make sure to remove those entries or else you will get errors like - /usr/bin/ld: cannot find "-lsybtcl" during make.
Start your Engines
Make changes to httpd.conf file and add php to the addtype directive, if you dont know how to do this click here
Start the Apache server and put the database connectivity test page in htdocs folder of apache. Modify the file and enter your database connectivity options. Load that page in your browser, it should display the servername of db server. If you see any errors make sure you have followed each and every step. If you still cannot figure out the solution post a comment below.
Method 2 - Using Sybase ASE, skip this part if you are using FreeTDS.
Configuring ASE
Download ASE linux, untar and run the setup file, start the GUI setup and install it to destination directory. For our guide I installed it to /usr/u/sumedh/ase Set the SYBASE environment variable to /usr/u/sumedh/ase
setenv SYBASE /usr/u/sumedh/ase
I dont why Sybase has not written any simple Readme or a tutorial for connecting with PHP, because by default PHP will spit errors during the configuration step when we try to connect with Sybase.
Sybase has changed the name of the library files from lib< name > to libsyb< name > to avoid name clashes with other libraries but the latest php version 5 still refers to the old files, this is a major source of problems with PHP Sybase connectivity. Sybase has provided a script which creates a symbolic link from the new library names to the old ones. It is located in /usr/u/sumedh/ase/OCS-15-0/scripts
Run the script
lnsyblibs create
If you want to remove these symbolic links then execute "lnsyblibs remove"
Just to be sure run isql and see if you can connect to your remote ASE server succesfully. if you can, proceed ahead, if not, put in a comment, I will try to help you out.
Configuring PHP
Make sure to set the $SYBASE environment variable.
setenv SYBASE /usr/u/sumedh/ase
./configure --prefix=/usr/u/sumedh/web5/php --with-apxs2=/usr/u/sumedh/web5/apache/bin/apxs --with-sybase=/usr/u/sumedh/ase/OCS-15_0
./configure --prefix=/usr/u/sumedh/web5/php --with-apxs2=/usr/u/sumedh/web5/apache/bin/apxs --with-sybase=/usr/u/sumedh/ase/OCS-15_0
Note - Make sure to set --with-sybase to /usr/u/sumedh/ase/OCS-15_0 and not /usr/u/sumedh/ase
Note - You have to give --with-sybase and not --with-sybase-ct, if you specify --with-sybase-ct, configure and make will not complain but when you actually run a php page with database connectivity you will get the following error.
Sybase: Unable to allocate connection record
Note - "--with-sybase" uses Sybase db_lib while --with-sybase-ct uses Sybase ct_lib. If for some reason db_lib does not work for you try using ct_lib.
Note - If you have not set the $SYBASE environement vaiable you will get the following error during make install
The context allocation routine failed when it tried to load localization files!!
One or more following problems may caused the failure
Your sybase home directory is /opt/sybase-11.9.2. Check the environment variable $SYBASE if it is not the one you want!
Cannot access file /opt/sybase-11.9.2/config/objectid.dat
PHP Fatal error: Unable to start sybase module in Unknown on line 0
One or more following problems may caused the failure
Your sybase home directory is /opt/sybase-11.9.2. Check the environment variable $SYBASE if it is not the one you want!
Cannot access file /opt/sybase-11.9.2/config/objectid.dat
PHP Fatal error: Unable to start sybase module in Unknown on line 0
Make sure $SYBASE is set when you start the apache server or else apache wont start and show the following error message in the log file.
The context allocation routine failed when it tried to load localization files!!
One or more following problems may caused the failure
Your sybase home directory is /opt/sybase-11.9.2. Check the environment variable SYBASE if it is not the one you want!
Cannot access file /opt/sybase-11.9.2/config/objectid.dat
[Wed Sep 23 17:49:54 2009] [notice] Apache/2.2.13 (Unix) PHP/5.2.6 configured -- resuming normal operations
[Wed Sep 23 18:03:57 2009] [notice] caught SIGTERM, shutting down
One or more following problems may caused the failure
Your sybase home directory is /opt/sybase-11.9.2. Check the environment variable SYBASE if it is not the one you want!
Cannot access file /opt/sybase-11.9.2/config/objectid.dat
[Wed Sep 23 17:49:54 2009] [notice] Apache/2.2.13 (Unix) PHP/5.2.6 configured -- resuming normal operations
[Wed Sep 23 18:03:57 2009] [notice] caught SIGTERM, shutting down
Start your Engines
Add php to the addtype directive in httpd.conf, if you dont know how to do this click here, start the Apache server, put the sample database connectivity test page in htdocs folder of apache. Enter the login details of your server and load that page in your browser, it should display the servername of your server. If you see any errors make sure you have followed each and every step. If you still cannot figure the solution post a comment below.
Sumedh, All rights reserved.