Serving Content from Adaptive Server Anywhere Databases Using Apache and PHP

Serving Content from Adaptive Server Anywhere Databases Using Apache and PHP

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, Linux, 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 Perl 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 C, Java, 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 php-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.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    23 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us