Serving Content from Adaptive Server Anywhere Databases Using Apache and PHP

A whitepaper from iAnywhere Solutions, Inc., a subsidiary of Sybase, Inc. Copyright © 2005 iAnywhere Solutions, Inc.

Contents

Introduction 2 What is PHP? ...... 2 How can I use PHP to connect to my database? ...... 2 Should I choose ODBC or the SQL Anywhere PHP module? ...... 2

Installing and configuring the SQL Anywhere PHP module (Windows, , Mac OS X) 3 Choosing which PHP module to use ...... 3 Installing the PHP module on Windows ...... 4 Installing the PHP module on Linux ...... 5 Loading the PHP driver dynamically ...... 6

Compiling the SQL Anywhere PHP module on UNIX 6 Requirements ...... 7 Adding the SQL Anywhere PHP module’s files to PHP’s source tree . . . 8 Compiling Apache and PHP ...... 8

Configuring the SQL Anywhere PHP module 12

Setting up PHP to connect to Adaptive Server Anywhere using an ODBC PHP module on UNIX 13

Using PHP with Adaptive Server Anywhere 15 Creating test files ...... 15 Determining your web server’s URL ...... 17 Starting the database server and web server ...... 17

Using the CGI executable version 19

Troubleshooting 20

Legal Notice 22 Contact Us ...... 22

1 Copyright © 2005 iAnywhere Solutions, Inc.

Introduction This whitepaper explains how to do the following: ♦ use the PHP scripting language to query your Adaptive Server Anywhere database. ♦ deliver the results of those queries via the World Wide Web using a Linux- or Mac OS X-based Apache web server.

It is assumed that you have a basic understanding of how to use Linux or Mac OS X (for example, of the tar and ls commands, and of bash or tcsh).

What is PHP?

PHP is a server side scripting language, like or ASP. The PHP manual (http://www.php.net/manual/ ) describes it as follows: ”PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from , , and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly.” One of PHP’s strengths is its support for the retrieval of data from a wide variety of databases, including Adaptive Server Anywhere databases. This support is manifested in its support for modules such as the SQL Anywhere PHP module, and ODBC driver managers such as iODBC and unixODBC.

How can I use PHP to connect to my database?

You can choose either of the following to make your system capable of accepting database requests through PHP: ♦ SQL Anywhere PHP module For information about using the PHP module, see “Installing and configuring the SQL Anywhere PHP module” on page 3. ☞ For information about compiling the SQL Anywhere PHP module from source code, see “Compiling the SQL Anywhere PHP module on UNIX” on page 6. ♦ ODBC For information about using the Adaptive Server Anywhere ODBC driver, see “Setting up PHP to connect to Adaptive Server Anywhere using an ODBC PHP module” on page 13.

Should I choose ODBC or the SQL Anywhere PHP module?

If you need to connect to other types of databases from PHP, or have already written many PHP scripts for use with ODBC, choose ODBC. Otherwise, it is recommended that you use the SQL Anywhere PHP module because it is simpler to set up and less prone to system resource leaks.

2 Copyright © 2005 iAnywhere Solutions, Inc.

Installing and configuring the SQL Anywhere PHP module (Windows, Linux, Mac OS X)

The following sections describe how to install and configure the SQL Anywhere PHP module.

Choosing which PHP module to use

If you are running on Windows or Linux, you can use the PHP module included in your SQL Anywhere Studio 9.0.2 install or download the appropriate module from the web site. On UNIX and Mac OS X, source files are provided for you to build the required libraries. ☞ For information about compiling the SQL Anywhere PHP module for UNIX, see “Compiling the SQL Anywhere PHP module on UNIX” on page 6. You can download the PHP module and source files from http://www.ianywhere.com/developer/code_samples/sqlany_php_module.html. On Windows, Adaptive Server Anywhere includes modules for PHP version 4 and PHP version 5. On Linux, Adaptive Server Anywhere includes both threaded and non-threaded modules for PHP version 4 and PHP version 5. In addition, for Linux and Mac OS X, PHP is supplied in a CGI version that can be used on the command line, and also as part of the Apache web server. If you are using the CGI version of PHP or if you are using Apache 1.x, use the non-threaded module. If you are using Apache 2.x, use the threaded module. The file names are as follows:

File name Description

-4.3.8_sqlanywhere9.dll PHP version 4.3.8 library for Windows php-4.3.11_sqlanywhere9.dll PHP version 4.3.11 library for Windows

php-4.4.0_sqlanywhere9.dll PHP version 4.4.0 library for Windows

php-5.0.2_sqlanywhere9.dll PHP version 5.0.2 library for Windows

php-5.0.4_sqlanywhere9.dll PHP version 5.0.4 library for Windows php-4.3.8_sqlanywhere9.so Non-threaded PHP version 4.3.8 library for Linux

php-4.3.11_sqlanywhere9.so Non-threaded PHP version 4.3.11 library for Linux

php-4.4.0_sqlanywhere9.so Non-threaded PHP version 4.4.0 library for Linux

php-5.0.2_sqlanywhere9.so Non-threaded PHP version 5.0.2 library for Linux

php-5.0.4_sqlanywhere9.so Non-threaded PHP version 5.0.4 library for Linux php-4.3.8_sqlanywhere9_r.so Threaded PHP version 4.3.8 library for Linux

3 Copyright © 2005 iAnywhere Solutions, Inc.

File name Description

php-4.3.11_sqlanywhere9_r.so Threaded PHP version 4.3.11 library for Linux

php-4.4.0_sqlanywhere9_r.so Threaded PHP version 4.4.0 library for Linux php-5.0.2_sqlanywhere9_r.so Threaded PHP version 5.0.2 library for Linux

php-5.0.4_sqlanywhere9_r.so Threaded PHP version 5.0.4 library for Linux

Installing the PHP module on Windows

To use the SQL Anywhere PHP module on Windows, you must copy the DLL from the SQL Anywhere installation directory and add it to your PHP installation. Optionally, you may then add an entry to your PHP initialization file to load the module, so you do not need to load it manually in each script.

❖ To install the PHP module on Windows 1. Locate the php.ini file of your PHP installation, and open it in a text editor. Locate the line that specifies the location of the extension_dir directory. 2. Copy the file phpX_sqlanywhere9.dll, where X is the PHP version number from the win32 subdirectory of your SQL Anywhere installation, to the directory specified by the extension_dir entry in the php.ini file.

3. Add the following line to the php.ini file to load the SQL Anywhere PHP driver automatically.

extension=phpX_sqlanywhere9.dll

An alternative to loading the PHP driver automatically is to load it dynamically in each script that requires it. ☞ For more information, see “Loading the PHP driver dynamically” on page 6. 4. At a command prompt, change to the root directory of your SQL Anywhere installation. Enter the following command to start the Adaptive Server Anywhere sample database:

dbeng9 asademo.db

This command starts a database server with a server name of asademo, as required by samples in this document. If you start the server from the Windows menu, it has a server name of asademo9, which is not compatible with the samples in this document. 5. At a command prompt, change to the src\php\examples subdirectory of your SQL Anywhere installation. Enter the following command:

php connect.php

The message Connected successfully should appear. If the php command is not recognized, verify that it is in your path.

4 Copyright © 2005 iAnywhere Solutions, Inc.

6. When you are done, stop the Adaptive Server Anywhere database server by clicking Shutdown in the Server Messages window.

Installing the PHP module on Linux

To use the SQL Anywhere PHP module on Linux x86, you must copy the shared object from the SQL Anywhere installation directory and add it to your PHP installation. Optionally, you may then add an entry to your PHP initialization file, php.ini, to load the module, so you do not need to load it manually in each script.

❖ To install the PHP module on Linux 1. Locate the php.ini file of your PHP installation, and open it in a text editor. Locate the line that specifies the location of the extension_dir directory.

2. Copy the shared object from the lib subdirectory of your SQL Anywhere installation to the directory specified by the extension_dir entry in the php.ini file. ☞ For information on which version of the shared object to use, see “Choosing which PHP module to use” on page 3.

3. Optionally, add the following line to the php.ini file to load the SQL Anywhere PHP driver automatically. Alternatively, you can load it manually with a few extra lines of code at the start of each script that requires it. The entry must identify the shared object you copied, which will be either

extension=phpX_sqlanywhere9.so

or, for the thread-safe shared object,

extension=phpX_sqlanywhere9_r.so

where X is the PHP version number. An alternative to loading the PHP driver automatically is to load it dynamically in each script that requires it. ☞ For more information, see “Loading the PHP driver dynamically” on page 6.

4. Before attempting to use the PHP module, verify that your environment is set up for Adaptive Server Anywhere. Depending on which shell you are using, enter the appropriate command to source the Adaptive Server Anywhere configuration script from the SQL Anywhere installation directory:

In this shell ...... use this command

sh, ksh, or bash . /bin/asa_config.sh

csh or tcsh source /bin/asa_config.csh

5. At a command prompt, change to the root directory of your SQL Anywhere installation. Enter the following command to start the Adaptive Server Anywhere sample database:

dbeng9 asademo.db

5 Copyright © 2005 iAnywhere Solutions, Inc.

6. At another command prompt, change to the src/php/examples subdirectory of your SQL Anywhere installation. Enter the following command:

php connect.php

The message Connected successfully should appear. If the php command is not recognized, verify that it is in your path.

Note If you do not have the src/php/examples subdirectory, you can download the samples from http://www.ianywhere.com/downloads/developer/phpsamples.- zip.

7. When you are done, stop the database server by pressing Q in the console window where it is running.

☞ For more information, see “Creating test files” on page 15 and “Configuring the SQL Anywhere PHP module” on page 12.

Loading the PHP driver dynamically

If the SQL Anywhere module is not always automatically loaded when PHP starts, you must prefix each script you write with the following lines of code. This code ensures that the SQL Anywhere PHP module is loaded.

$module_name= # Ensure that the SQL Anywhere PHP module is loaded if( !extension_loaded(’sqlanywhere’) ) { # Find out which version of PHP is running $version=phpversion; if( strtoupper(substr(PHP_OS, 0, 3) == ’WIN’) ) { dl( $module_name.’.dll’ ); } else { dl( $module_name.’.so’ ); } }

Note In threaded environments, it is recommended that you do not load the PHP driver dynamically. Instead, use the extensions statement in your php.ini when operating under such an environment.

Compiling the SQL Anywhere PHP module on UNIX To connect PHP to Adaptive Server Anywhere using the SQL Anywhere PHP module on UNIX, you must add the SQL Anywhere PHP module’s files to PHP’s source tree, and then re-compile PHP.

6 Copyright © 2005 iAnywhere Solutions, Inc.

Requirements

The following is a list of software you need to have on your system to complete the steps detailed in this document: ♦ Adaptive Server Anywhere 8.0.x or 9.0.x, which may run on the same computer as the Apache web server, or on a different computer. ♦ If you plan to use the SQL Anywhere PHP module, it is available at http://www.ianywhere.com/developer/code_samples/sqlany_php_module.html. You also need sqlpp and libdblibX.so (Linux) or libdblibX.dylib.1 (Mac OS X) installed (check your Adaptive Server Anywhere lib directory) where X is either 8 or 9, depending on your version of Adaptive Server Anywhere. sqlpp is installed with the embedded SQL and UltraLite Development installation options for Adaptive Server Anywhere.

♦ The PHP source code, which can be downloaded from http://www.php.net. The following versions of PHP are supported: • 4.3.8 • 4.3.11 • 4.4.0 • 5.0.2 • 5.0.4 ♦ The Apache web server source code, which can be downloaded from http://httpd.apache.org. If you are going to use an already-compiled version of Apache, make sure you have apache and apache-devel installed. ♦ If you plan to use the Unified ODBC PHP module, you need to have libdbodbcX.so installed (check your Adaptive Server Anywhere lib directory). The following binaries should be installed from your Linux installation disk if they are not already installed, and can be found as RPMs: ♦ make ♦ automake ♦ autoconf

♦ libtool (glibtool for Mac OS X) ♦ makeinfo ♦ bison

♦ gcc ♦ cpp ♦ glibc-devel

7 Copyright © 2005 iAnywhere Solutions, Inc.

♦ kernel-headers ♦ flex You must have the same access privileges as the person who installed PHP to perform certain steps of the installation. Most UNIX-based systems offer a sudo command that allows users with insufficient permissions to execute certain commands as a user with the right to execute them.

Adding the SQL Anywhere PHP module’s files to PHP’s source tree

1. Download the SQL Anywhere PHP module from http://www.ianywhere.com/developer/code_samples/sqlany_php_module.html. 2. From the directory where you saved the SQL Anywhere PHP module, untar the files to the ext subdirectory of the PHP source tree (Mac OS X users should replace tar with gnutar):

$ tar -xzf sqlanywhere_php-version.tar.gz -C PHP-source- directory/ext/ For example:

$ tar -xzf sqlanywhere_php-1.0.8.tar.gz -C ~/php-4.3.x/ext

3. Make PHP aware of the module:

$ cd PHP-source-directory/ext/sqlanywhere $ touch * $ cd ~/PHP-source-directory $ ./buildconf The following example is for PHP version 4.3.x. You must change php-4.3.x to the version of PHP you are using.

$ cd ~/php-4.3.x/ext/sqlanywhere $ touch * $ cd ~/php-4.3.x $ ./buildconf

4. Verify that PHP is aware of the module:

$ ./configure -help | egrep sqlanywhere

If you were successful in making PHP aware of the SQL Anywhere module, you should see the following text:

--with-sqlanywhere=[DIR] If you are unsuccessful, keep track of the output of this command and post it to the sybase.public.sqlanywhere.linux newsgroup for assistance.

Compiling Apache and PHP

PHP can be compiled as a shared module of a web server (such as Apache) or as a CGI executable. If you are using a web server that is not supported by PHP,

8 Copyright © 2005 iAnywhere Solutions, Inc.

or if you want to execute PHP scripts in your command shell rather than on a web page, you should compile PHP as a CGI executable. Otherwise, if you want to install PHP to operate in conjunction with Apache, compile it as an Apache module. ☞ For information about compiling PHP as an Apache module, see “Compiling PHP as an Apache module” on page 9. ☞ For information about compiling PHP as a CGI executable, see “Compiling PHP as a CGI executable” on page 11. Compiling PHP as an Apache module

The first two steps in the following instructions configure Apache so that it recognizes shared modules. If you have a compiled version of Apache already installed on your system, proceed to step 3. Note that Mac OS X comes with a preinstalled Apache web server.

❖ To compile PHP as an Apache module 1. Configure Apache to recognize shared modules. Execute the following command (entered all on one line) from the directory where your Apache files were extracted:

$ cd Apache-source-directory $ ./configure --enabled-shared=max --enable-module=most -- prefix=/Apache-installation-directory

The following example is for Apache version 1.3.28. You must change apache_1.3.28 to the version of Apache you are using.

$ cd ~/apache_1.3.28 $ ./configure --enabled-shared=max --enable-module=most -- prefix=/usr/local/web/apache

2. Recompile and install the relevant components:

$ make $ make install Now you are ready to compile PHP to operate as an Apache module. 3. Make sure the environment is set up for Adaptive Server Anywhere. Depending on which shell you are using, enter the appropriate command from the directory where SQL Anywhere Studio was installed (by default, this is /opt/sybase/SYBSsa9/ ). On Mac OS X, the default directory is /Applications/SQLAnywhere9/System.

If you’re using this shell...... use this command

sh, ksh, bash . ./bin/asa_config.sh

csh, tcsh source ./bin/asa_config.csh

4. Configure PHP as an Apache module to include the SQL Anywhere PHP module.

9 Copyright © 2005 iAnywhere Solutions, Inc.

Execute the following commands:

$ cd PHP-source-directory $ ./configure --with-sqlanywhere --with- apxs=/Apache- installation-directory/bin/apxs

The following example is for PHP version 4.3.x. You must change php-4.3.x to the version of PHP you are using.

$ cd ~/php-4.3.x/ $ ./configure --with-sqlanywhere --with- apxs=/usr/local/web/apache/bin/apxs

The configure script will try to determine the version and location of your Adaptive Server Anywhere installation. In the output from the command, you should see lines similar to the following:

checking for SQL Anywhere support... yes checking SQL Anywhere install dir... /opt/sybase/SYBSsa9 checking SQL Anywhere version... 9

5. Recompile the relevant components:

$ make

6. Check that the libraries are properly linked. ♦ Linux users (the following example assumes you are using PHP version 4):

ldd ./.libs/libphp4.so

♦ Mac OS X users: Refer to your httpd.conf configuration file to determine where libphp4.so is on your computer. Perform the check with the following command:

otool -L $LIBPHP4_DIR/libphp4.so $LIBPHP4_DIR is the directory where libphp4.so is located, according to your server configuration. This command outputs a list of the libraries that libphp4.so uses. Verify that libdblib9.so is in the list.

7. Install the PHP binaries in Apache’s lib directory:

$ make install

8. Perform verification. PHP does this automatically. All you need is to make sure that your httpd.conf configuration file is verified so that Apache will recognize .php files as PHP scripts. httpd.conf is stored in the conf subdirectory of the Apache directory:

$ cd Apache-installation-directory/conf

For example:

$ cd /usr/local/web/apache/conf

Make a backup copy of httpd.conf before editing the file (you can replace pico with the text editor of your choice):

10 Copyright © 2005 iAnywhere Solutions, Inc.

$ cp httpd.conf httpd.conf.backup $ pico httpd.conf

Add or uncomment the following lines in httpd.conf (they are not located together in the file):

LoadModule php4_module libexec/libphp4.so AddModule mod_php4.c AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps

Note On Mac OS X, the last two lines should be added or uncommented in httpd_- macosxserver.conf. The first two lines point Apache to the files that are used for interpreting PHP code, while the other two lines declare file types for files whose extension is .php or . so Apache can recognize and deal with them appropriately. ☞ For information about testing and using your setup, see “Using PHP with Adaptive Server Anywhere” on page 15.

Compiling PHP as a CGI executable

❖ To compile PHP as a CGI executable 1. Make sure the environment is set up for Adaptive Server Anywhere. ☞ For instructions on setting up the environment for Adaptive Server Anywhere, follow the instructions in step 4 of “Compiling PHP as an Apache module” on page 9.

2. Configure PHP as a CGI executable and with the SQL Anywhere PHP module. Execute the following command from the directory where your PHP files were extracted:

$ cd PHP-source-directory $ ./configure --with-sqlanywhere

For example:

$ cd ~/php-4.3.x/ $ ./configure --with-sqlanywhere

The configuration script will try to determine the version and location of your Adaptive Server Anywhere installation. If you examine the output of this command, you should see lines similar to the following:

checking for SQL Anywhere support... yes checking SQL Anywhere install dir... /opt/sybase/SYBSsa9 checking SQL Anywhere version... 9

3. Compile the executable:

$ make

11 Copyright © 2005 iAnywhere Solutions, Inc.

4. Install the components.

$ make install

☞ For information on testing and using PHP, see “Using PHP with Adaptive Server Anywhere” on page 15.

Configuring the SQL Anywhere PHP module

The behavior of the SQL Anywhere PHP driver can be controlled by setting values in the PHP initialization file, php.ini. This file is included in your PHP installation. The following entries are currently supported. ♦ allow_persistent Allows persistent connections when set to 1. It does not allow them when set to 0. The default value is 1.

sqlanywhere.allow_persistent=1

♦ max_persistent Sets the maximum number of persistent connections. The default value is -1, which means no limit.

sqlanywhere.max_persistent=-1

♦ max_connections Sets the maximum number of connections that can be opened at once through the SQL Anywhere PHP module. The default value is -1, which means no limit.

sqlanywhere.max_connections=-1

♦ auto_commit Specifies whether the database server performs a commit operation automatically. The commit is performed immediately following the execution of each statement when set to 1. When set to 0, transactions should be ended manually with either the sqlanywhere_commit or sqlanywhere_rollback function, as appropriate. The default value is 1.

sqlanywhere.auto_commit=1

♦ row_counts Returns the exact number of rows affected by an operation when set to 1 or an estimate when set to 0. The default value is 0.

sqlanywhere.row_counts=0

♦ verbose_errors Returns verbose errors when set to 1. Otherwise, you must call the sqlanywhere_error or sqlanywhere_errorcode function to get further error information. The default value is 1.

sqlanywhere.verbose_errors=1

☞ For more information, see sqlanywhere_set_option function at http://www.ianywhere.com/developer/product_- manuals/sqlanywhere/9.0/php/html/set_option.html.

12 Copyright © 2005 iAnywhere Solutions, Inc.

Setting up PHP to connect to Adaptive Server Anywhere using an ODBC PHP module on UNIX If you will be using other ODBC drivers in addition to Adaptive Server Anywhere’s ODBC driver, you need to install an ODBC driver manager (such as iODBC or unixODBC). Once you have installed the driver manager of your choice and compiled PHP with the appropriate PHP Module (see the documentation accompanying PHP or the driver manager), you need to do two things in your ODBC driver manager to access your data: ♦ set up the Adaptive Server Anywhere ODBC driver, dbodbc9.so ♦ create an ODBC data source If you do not need to connect to databases using other ODBC drivers, however, Adaptive Server Anywhere’s ODBC driver includes the functionality of a driver manager, so you can use ODBC without installing and setting up a driver manager.

❖ To set up PHP to access Adaptive Server Anywhere via ODBC without an ODBC driver manager 1. Go to the Adaptive Server Anywhere lib directory:

$ cd SQL-Anywhere-installation-directory/lib

For example:

$ cd /opt/sybase/SYBSsa9/lib

On Mac OS X, go to /Applications/SQL Anywhere 9/System/lib. 2. To make your system recognize the Adaptive Server Anywhere ODBC driver as a driver manager, you need to set up the following symbolic links:

$ ln -s dbodbc9.so libodbc.so $ ln -s dbodbc9.so libodbc.so.1 $ ln -s dbodbc9.so libodbcinst.so $ ln -s libdbodbc9.so libodbcinst.so.1 ♦ On Mac OS X, these files have the extension .bundle. ♦ On HP, these files have the prefix lib and the extension .sl. 3. Set up the following environment variables:

$ export CFLAGS="-DODBC_UNIX" $ export CUSTOM_ODBC_LIBS="-lodbc"

4. Configure PHP as an Apache module and with the Unified ODBC PHP module by executing the following commands:

13 Copyright © 2005 iAnywhere Solutions, Inc.

$ cd PHP-source-directory $ ./configure --with-apxs=/Apache-installation- directory/bin/apxs --with-custom-odbc=/SQL-Anywhere-installation-directory/ Example:

$ cd ~/php-4.3.x $ ./configure --with-apxs=/usr/local/web/apache/bin/apxs --with-custom-odbc=/opt/sybase/SYBSsa9/

5. Recompile the relevant components:

$ make

6. Check that the libraries are properly linked. ♦ Linux users should use the following command (the following example assumes you are using PHP version 4):

ldd ./.libs/libphp4.so ♦ Mac OS X users should refer to the httpd.conf configuration file to determine where libphp4.so is on their computer. Perform the check with the following command:

otool -L $LIBPHP4_DIR/libphp4.so $LIBPHP4_DIR is the directory storing libphp4.so, according to your server configuration. This command outputs a list of the libraries that libphp4.so will be using. Verify that the libodbc.so is in that list. 7. Install the PHP binaries in Apache’s lib directory:

$ make install

8. Set up the environment so that Apache can load the Adaptive Server Anywhere ODBC driver:

$ export ODBC_INI=path-to-driver.odbc.ini For example:

$ export ODBC_INI=~/.odbc.ini

Depending on which shell you’re using, enter the appropriate command from the directory where SQL Anywhere Studio was installed (by default, this is /opt/sybase/SYBSsa9/ ). ☞ For information about which command to use, follow the instructions in step 4 of “Compiling PHP as an Apache module” on page 9. 9. Edit the httpd.conf configuration file so that Apache will recognize .php files as PHP scripts. httpd.conf is stored in the conf subdirectory of the Apache directory. First, move to conf :

$ cd Apache-installation-directory/conf For example:

14 Copyright © 2005 iAnywhere Solutions, Inc.

$ cd /usr/local/web/apache/conf

It is recommended that you make a backup copy of httpd.conf before editing the file:

$ cp httpd.conf httpd.conf.backup $ pico httpd.conf

Once you have httpd.conf open, add or uncomment the following lines (they are not located together in the file:

LoadModule php4_module libexec/libphp4.so AddModule mod_php4.c AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps

☞ For information about testing and using your setup, see “Using PHP with Adaptive Server Anywhere” on page 15.

Using PHP with Adaptive Server Anywhere

This section includes instructions for using the Apache version of PHP and for the CGI Executable version of PHP. This section contains three subsections: ♦ “Creating test files” on page 15 contains some PHP code you can use to test your PHP setup. ♦ “ Your web server’s URL” on page 17 helps you determine what URL to type in your browser to access documents.

♦ “ How to start the database and web servers” on page 17 describes how to start up the Adaptive Server Anywhere database server and the Apache server.

♦ SQL Anywhere PHP module API reference For reference information about the functions provided in the SQL Anywhere PHP module API, see http://www.ianywhere.com/developer/product_- manuals/sqlanywhere/9.0/php/html/api_main.html.

Creating test files

The following instructions apply to all configurations.

❖ To create a test file 1. Create a file to test whether PHP is set up properly by calling phpinfo(). 2. Place a file containing the following PHP code in the directory that stores web content on your system and name it info.php.

15 Copyright © 2005 iAnywhere Solutions, Inc.

phpinfo() is a PHP function that generates a page of system setup information, which indicates that PHP is working. ♦ On Linux, use the htdocs directory. ♦ If you are the System Administrator on a Mac OS X system, use /Library/WebServer/Documents. ♦ If you are a Mac OS X user, place the file in /Users/[your-username]/Sites/.

For PHP with the SQL Anywhere PHP module (Windows and UNIX)

You must now create a file that starts a connection to the database, queries it, returns the results, and then disconnects from the database.

❖ To create the file 1. Place a file containing the following PHP code in the web content directory for your system (see above) and name it asa_test.php:

For PHP with the unified ODBC PHP module (UNIX)

Prior to using ODBC connections, you need to create an ODBC data source. Using dbdsn, you can create a dsn as follows:

dbdsn -w my_test_dsn -c "UID=dba;PWD=sql;DRIVER=/opt/sybase/ SYBSsa9/lib/libdbodbc9.so;DBF=/tmp/asademo.db"

On Mac OS X, use the following command (entered all on one line):

dbdsn -w my_test_dsn -c "UID=dba;PWD=sql;DRIVER=/Applications/ SQLAnywhere9/lib/dbodbc9.bundle;DBF=/tmp/asademo.db"

☞ For more information about ODBC connections, refer to the topic “Using ODBC data sources on UNIX” in the Adaptive Server Anywhere Database Administration Guide in your SQL Anywhere Studio documentation or at http://www.ianywhere.com/developer/product_- manuals/sqlanywhere/0902/en/html/dbdaen9/00000079.htm. Now you need to create a file that starts a connection to the database, queries it, returns the results, and then disconnects from the database.

❖ To create the file 1. Place a file containing the following PHP code in the web content directory for your system and name it asa_test_odbc.php:

16 Copyright © 2005 iAnywhere Solutions, Inc.

Note If the Apache web server is running as a Windows or Linux service (daemon) or a Mac OS X start up item, it runs in a different environment. You must configure the environment for the Linux or Windows service or Mac OS X start up item using the steps in “Starting the database server and web server” on page 17.

Make sure you have a copy of the asademo.db demo database in your home directory. It is installed with Adaptive Server Anywhere.

Determining your web server’s URL

The URL of your web server is of the form http://hostname[:server-port]/. You can find hostname simply by executing the hostname command at a command prompt, or you can use your computer’s network IP address if you know it. The value for server-port is usually 80 or 8080. If you want to look at a file named test.html on the web server, for example, and if executing hostname gives the output helios, you might use the URL http://helios:8080/test.html to view the file.

Starting the database server and web server

Windows users: start at step 2. UNIX, Linux, and Mac OS X users: start at step 1.

❖ To start the database server and web server 1. Before you begin, make sure the environment is set up for Adaptive Server Anywhere. Depending on which shell you’re using, enter the appropriate command from the directory where SQL Anywhere Studio was installed (by default, this is /opt/sybase/SYBSsa9/ ):

If you’re using this shell...... use this command

sh, ksh, bash . . /bin/asa_config.sh

source ./bin/asa_config.csh csh, tcsh source

17 Copyright © 2005 iAnywhere Solutions, Inc.

Note If you are using ODBC, you need to set up the environment so Apache can load the Adaptive Server Anywhere ODBC driver. Execute the following command:

exportODBCI NI = path-to-driver.odbc.ini

2. Start the Apache web server. ♦ On Windows, use the following command:

Apache-installation-directory\apachectl start

♦ On Linux, use the following command:

$ /Apache-installation-directory/bin/apachectl start For example:

$ /usr/local/web/apache/bin/apachectl start

♦ On Mac OS X, use the following command:

$ /usr/sbin/apachectl start

3. If you are testing your setup and have created the files as instructed in “Creating test files” on page 15, use the following URL to test PHP:

http://hostname[:server-port]/info.php

If everything was successful, you should see a page displaying information about your web server setup. If you are using the SQL Anywhere PHP module, there should be a section on the SQL Anywhere module as well. 4. Start the database you want to access using PHP. For example, if you are testing your setup, use the following command:

$ dbeng9 asademo.db

5. Check that PHP can access databases by attempting a query to Adaptive Server Anywhere’s sample database. If you created the files as instructed in “Creating test files” on page 15 and you are using the SQL Anywhere PHP module, use the following URL:

http://hostname[:server-port]/asa_test.php

If you’re using the Unified ODBC PHP module, use the following URL:

http://hostname[:server-port]/asa_test_odbc.php

If everything was successful, you should see the contents of the employee table in your browser when you open the above URL.

6. When you are done, stop the database server by pressing Q in the console window, and then stop the web server. ♦ On Linux, use the following command:

$ /Apache-installation-directory/bin/apachectl stop

18 Copyright © 2005 iAnywhere Solutions, Inc.

For example:

$ /usr/local/web/apache/bin/apachectl stop On Mac OS X, use the following command:

$ /usr/sbin/apachectl stop

Using the CGI executable version

If you are running on Windows, the following steps assume that PHP has already been added to your Path.

❖ To use the CGI executable version 1. Before you begin, make sure the environment is set up for Adaptive Server Anywhere. Depending on which shell you’re using, enter the appropriate command from the directory where SQL Anywhere Studio was installed (by default, this is/opt/sybase/SYBSsa9/ ). ☞ For instructions about which command to use, see step 4 of “Compiling PHP as an Apache module” on page 9. 2. Start the database you want to access using PHP. For example, if you’re testing your setup on UNIX, use the following command:

$ dbeng9 asademo.db On Windows, use the following command:

dbeng9 asademo.db

3. In a new console window, run the script using the php executable you compiled. On UNIX, use the following command:

$ cd PHP-source-directory $ ./php name-and-location-of-php-script For example, if you are testing your setup using PHP version 4.3.x:

$ cd ~/php-4.3.x $ ./php ext/sqlanywhere/examples/connect.php On Windows, use the following command:

cd PHP-source-directory ./php name-and-location-of-php-script For example, if you are testing your setup using PHP version 4.3.x:

cd ~\ php-4.3.x ./php \ examples\ connect.php

If everything is successful, you should see the message Connected successfully.

4. When you are done, stop the database server by pressing Q in the console window where it is running.

19 Copyright © 2005 iAnywhere Solutions, Inc.

Troubleshooting

♦ Executing a make or make install gives me errors • Try using the make clean command before trying again. • Make sure you have all the components installed that are listed in “Requirements” on page 7.

♦ Using dbdsn to create a new data source gives the error Could not open config file ~/.odbc.ini • Make sure that a file named .odbc.ini (empty or containing other data sources) exists in your home directory, and that you have write permissions on it. • Refer to your SQL Anywhere Studio documentation for additional information about the dbdsn utility.

♦ When I try to execute make install, I get a lot of Permission Denied errors • make install is trying to install files where it shouldn’t be trying to install them, or you do not have sufficient access privileges. If you know that you installed PHP in a different location than where make install is attempting to put its files, use the -prefix=location-where-PHP was-installed option with ./configure to specify a location. If you were not the person who installed PHP, you might have to install a copy of PHP in your own disk space and start over, or use su to temporarily log in as the other user (sometimes root). ♦ I can get the PHP command line to work, but I cannot get Apache to work with the SQL Anywhere PHP driver You probably have set up your user shell environment described in this document (sourcing asa_config.sh setting the ODBCINI environment variable) and this is why the PHP command works. The Apache web server is likely running as a Windows or Linux service (daemon) or Mac OS X start up item and is running in a different environment. You must need configure the environment for the Linux or Windows service or Mac OS X startup item as you did in the shell. ☞ For more information, see “Starting the database server and web server” on page 17. ♦ I am unable to use the pre-compiled SQL Anywhere PHP module and am getting the following errors:

Warning: dl(): sqlanywhere: Unable to initialize module Module compiled with module API=X, debug=0, thread-safety=0 PHP compiled with module API=X, debug=0, thread-safety=0

(In the above example, X is the version number.) You are probably using a version PHP that is earlier or later than the version supported by the pre-built PHP module. See “Choosing which PHP module to use” on page 3 for a list of PHP versions that are supported for use with the

20 Copyright © 2005 iAnywhere Solutions, Inc.

PHP module included with SQL Anwhere Studio. Alternatively, you can recompile the PHP driver source with your version of PHP to get the SQL Anywhere driver working with your version of PHP. ♦ For more help, you can look in the following places: • On-line help is available on the SQL Anywhere Studio CD-ROM. If your computer is set up to mount the CD-ROM to /mnt/cdrom/, the help will be located in /mnt/cdrom/help/contents.htm. Open it in any web browser that supports tables, for example, Netscape Navigator. Style sheets support is recommended, but is not necessary. • A FAQ is available for the UNIX version of Adaptive Server Anywhere at http://www.sybase.com/detail?id=1011965. • Check if there have been any bug fixes or updates posted at http://downloads.sybase.com/swx/sdmain.stm. • The Newsgroups sybase.public.sqlanywhere.general and sybase.public.sqlanywhere.linux are most likely to be relevant, and can be read from the web or with a newsgroup reader. Be sure to look through old threads for your problem, as it may already have been addressed. If you use a newsgroup reader, the newsgroups are available on forums.sybase.com. On the web, they can be viewed at http://www.sybase.com/support/newsgroups.

21 Copyright © 2005 iAnywhere Solutions, Inc.

Legal Notice

Copyright © 2005 Sybase, Inc. All rights reserved. Sybase, the Sybase logo, Adaptive Server, MobiLink, and SQL Anywhere are trademarks of Sybase, Inc. All other trademarks are property of their respective owners. The information, advice, recommendations, software, documentation, data, services, logos, trademarks, artwork, text, pictures, and other materials (collectively, “Materials”) contained in this document are owned by Sybase, Inc. and/or its suppliers and are protected by copyright and trademark laws and international treaties. Any such Materials may also be the subject of other intellectual property rights of Sybase and/or its suppliers all of which rights are reserved by Sybase and its suppliers. Nothing in the Materials shall be construed as conferring any license in any Sybase intellectual property or modifying any existing license agreement. The Materials are provided “AS IS”, without warranties of any kind. SYBASE EXPRESSLY DISCLAIMS ALL REPRESENTATIONS AND WARRANTIES RELATING TO THE MATERIALS, INCLUDING WITHOUT LIMITATION, ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. Sybase makes no warranty, representation, or guaranty as to the content, sequence, accuracy, timeliness, or completeness of the Materials or that the Materials may be relied upon for any reason. Sybase makes no warranty, representation or guaranty that the Materials will be uninterrupted or error free or that any defects can be corrected. For purposes of this section, ‘Sybase’ shall include Sybase, Inc., and its divisions, subsidiaries, successors, parent companies, and their employees, partners, principals, agents and representatives, and any third-party providers or sources of Materials.

Contact Us

iAnywhere Solutions Worldwide Headquarters One Sybase Drive, Dublin, CA, 94568 USA Phone 1-800-801-2069 (in US and Canada) Fax 1-519-747-4971 World Wide Web http://www.ianywhere.com E-mail [email protected]

22