Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Purpose

The purpose of this page is to provide an introduction to using PHP and SQL Anywhere to create web applications with rich dynamic content and functionality.

Overview

PHP is a popular server-side scripting language primary used for web development. PHP is an HTML-embedded scripting language that uses syntax from C, Java and Perl. There are a few unique PHP-specific features as well. The goal of the language is to let web developers create dynamically generated pages more quickly.

Requirements

  • A web server such as Apache, IIS, or any web server that supports PHP.
  • PHP installed on the same computer as the web server.
  • SQL Anywhere installed on th esame computer as the web server. The client software can be used as well.
  • SQL Anywhere PHP extension

Installing a Web Server and PHP

Apache is available for download from the Apache website and PHP can be downloaded from the PHP website. Apache and PHP can also be obtained as part of distribution ssuch as XAMPP, WampServer, Linux packages or some Linux distributions. Many of these distributions install Apache and PHP pre-configured, making it possible to get Apache and PHP up and running quickly and requiring less setup than installing them separately.

Detailed setup instructions for web servers and PHP are not included here. There are many resources on the Internet that explain how to set up Apache or IIS with PHP. You should accept the default installation options if you are unfamiliar with setting up a web server.

By default, your website is a single root directory and subdirectories on your file system. Using IIS, the default root directory will be c:\inetpub\wwwroot. Using Apache, the root directory is controlled by the DocumentRoot configuration option. the default value for DocumentRoot varies between distributions and the location for setting DocumentRoot also varies (usually in a file named httpd.conf). Many distributions of Apache have a default webpage at http://localhost with a link to a phpinfo() page. Click this phpinfo() link and search for DOCUMENT_ROOT to determine the Apache DocumentRoot setting.

Running a PHP script is as simple as copying the PHP source file to a directory within your website and then referencing the file in your browser. The web server will recognize the .php file extension and automatically use the PHP interpreter to generate HTML output for your browser. Try creating the following PHP script in the root directory of your local website. Name the file phpinfo.php. Run this script by viewing http://localhost/phpinfo.php in your browser. Your web server needs to be running.

<html>
<body>
<?php
     phpinfo();
?>
</body>
</html>

You can also run PHP from the command line. This can be useful when troubleshooting. The command php -i will output the same PHP environment information as calling phpinfo() in a PHP script. The PHP executable needs to either be in your PATH or you will need a fully qualified filename for the PHP program.

Installing SAP SQL Anywhere

Obtain a copy of SAP SQL Anywhere via the SAP Store. Product documentation can be downloaded separately or you can use the online documentation.

You should check for SQL Anywhere updates to make sure you have the most recent bug fixes. You will need to register to be able to download the bug fixes. To check for SQL Anywhere updates:

  • On Windows, choose Start > All Programs > SQL Anywhere ## > Check for Updates.
  • On Linux with GNOME desktop, choose Applications > SQL Anywhere ## > Check for Updates.
  • On all operating systems, open updchk.html in the support folder of your SQL Anywhere installation.

SQL Anywhere PHP Extension Module

PHP extensions enhance PHP by making additional programming interfaces available to PHP programs. The SQL Anywhere PHP extension module provides a PHP programming interface for SQL Anywhere.

PHP extensions are implemented as shared libraries (DLLs on Windows). The naming convention for the SQL Anywhere PHP extension depends on the platform. There is a different extension module for each version of PHP. The PHP version is identified in the file name. If your version of PHP is more recent than the most recent SQL Anywhere PHP extension module, use the most recent SQL Anywhere PHP module. For example, you can use the SQL Anywhere extension for PHP 5.2.5 even though you are using PHP 5.2.6.

The name of the PHP extension on Linux and Solaris is php-5.x.y_sqlanywhere[_r].so. The name of the PHP extension on Mac OS X is php-5.x.y_sqlanywhere[_r].dylib. The threaded versions of the library are indicated by _r. You should use the threaded version unless you understand a reason why you need the unthreaded version. You must use the threaded version when PHP Thread Safety is enabled. You can use either version when PHP thread safety is disabled.

The name of the PHP extension on Windows is php-5.x.y_sqlanywhere.dll. The Windows extension is threaded.

Installing the PHP Module on Windows

  1. Locate the PHP configuration file (php.ini). The best way to locate php.ini is by searching phpinfo() output for php.ini, or for Loaded Configuration File. The location may be different when using PHP from the command line than when using PHP within Apache or IIS.
  2. Edit php.ini using a text editor and search for extension_dir. This indicates a directory on the file system where PHP extension modules must be copied. If extension_dir is not set to a directory, create a new subdirectory in your PHP install and name it extensions, then modify php.ini to set extension_dir to the fully qualified name of the new directory.
  3. Download and extract the SQL Anywhere PHP extension module. You should get a file named php-5.x.y_sqlanywhere.dll. Copy the file to the directory specified by the extension_dir entry in the php.ini file.
  4. To load the SQL Anywhere PHP extension automatically whenever PHP starts, add one line to the Dynamic Extensions section of the php.ini file:
         extension=php-5.x.y_sqlanywhere.dll
  5. Modify your environment to allow the SQL Anywhere PHP extension to use libraries located within the SQL Anywhere intallation. The bin32 subdirectory of the SQL Anywhere installation must be in your PATH. You can modify the Windows PATH using the Control Panel. You can check PATH by looking in the Environment section of the phpinfo() output.
  6. Restart your web server. You can check the status of the SQL Anywhere extension by searching for sqlanywhere in the phpinfo() output.

Installing the PHP Module on Linux, Mac OS X, or Solaris

  1. Locate the PHP configuraiton file (php.ini). The best way to locate php.ini is by searching phpinfo() output for php.ini, or for Loaded Configuration File. The location may be different when using PHP from the command line than when using PHP within Apache.
  2. Edit php.ini using a text editor and search for extension_dir. This indicates a directory on the file system where PHP extension modules must be copied. If extension_dir is not set to a directory, create a new subdirectory in your PHP install and name it extensions. Then modify php.ini to set extensions_dir to the fully qualified name of the new direcotry.
  3. Download and extract the SQL Anywhere PHP extension module. You should get a file named php-5.x.y_sqlanywhere_r.so. Copy the file to the directory specified by the extension_dir entry in the php.ini file.
  4. To load the SQL Anywhere PHP extension automatically whenever PHP starts, add one line to the Dynamic Extensions section of the php.ini file:
         extension=php-5.x.y_sqlanywhere_r.so
  5. Modify your environment to allow the SQL Anywhere PHP extension to use libraries located within the SQL Anywhere installation. The lib32 or lib64 subdirectory of the SQL Anywhere installation must be in LD_LIBRARY_PATH. You can modify LD_LIBRARY_PATH by changing the envvars file of your Apache installation. You can check LD_LIBRARY_PATH by looking in the Environment section of the phpinfo() output. For example, add the following lines to envvars (using sh, ksh, or bash):
         LD_LIBRARY_PATH="/opt/sqlanywhere16/lib32:$LD_LIBRARY_PATH"
         export LD_LIBRARY_PATH
  6. Restart your web server. You can check the status of the SQL Anywhere extension by searching for sqlanywhere in the phpinfo() output.

Trying PHP with SQL Anywhere

Once the SQL Anywhere PHP extension module is running, you should try a PHP script that uses SQL Anywhere. Before you can do that, you need to create a SQL Anywhere database and have SQL Anywhere running. Open a command prompt and change the current directory to a place where you would like to create a SQL Anywhere database file. Once you are there, dbinit will create a database and dbsrv16 will start the SQL Anywhere server.

From the command line on Windows:

"\Program Files\SQL Anywhere 16\Bin32\dbinit" hits.db
"\Program Files\SQL Anywhere 16\Bin32\dbsrv16" hits.db

From the command line on non-Windows platforms:

/opt/sqlanywhere/bin32s/dbinit hits.db
/opt/sqlanywhere/bin32s/dbsrv16 hits.db

You can also create databases and start servers using Sybase Central. The server can also run in the background as a service or daemon, but running the server from the command line keeps it straight-forward to start.

Create a new PHP file named hits.php in the root directory of your local website. Use the following script as the content of the new file:

<html>
<body>
<p>Record page hits</p>
<?php
     $conn = sasql_connect("uid=dba;pwd=sql");
     sasql_query($conn,
          "CREATE TABLE IF NOT EXISTS hits (hit TIMESTAMP)");
     sasql_query($conn,
          "INSERT INTO hits VALUES(now(*))";
     $result = sasql_query($conn,
          "SELECT TOP 10 hit FROM hits ORDER BY hit DESC");
     if ($result) {
          echo "<table border='1'>\n";
          while ($obj = sasql_fetch_object($result)) {
               echo "<tr>\n";
               echo "<td>$obj->hit</td>\n";
               echo "</tr>\n";
          }
          sasql_free_result($result);
          echo "</table>\n";
     }
     sasql_close($conn);
?>
</body></html>

Run the script by viewing http://localhost/hits.php in your browser. Your web server needs to be running. The first time the script runs, it will create a table called hits and insert one row. Each subsequent time the script runs, it wil insert a new row in the table and output an HTML page showing the ten most recent rows.

Troubleshooting

Try restarting your web server. This can be useful when you have made configuration changes that seem to have no effect.

Try restarting your browser. Sometimes, browsers are caching old information.

Review information in the phpinfo() output. This can be useful when you are having trouble determining why your PHP script is not working.

  • Check your environment for SQL Anywhere in the path (PATH or LD_LIBRARY_PATH).
  • Check the location of the loaded configuration file (php.ini). It can be in a different location running PHP from the command line.
  • Check the location of website files (DOCUMENT_ROOT).
  • Check the status of the SQL Anywhere PHP extension (sqlanywhere).

Your PHP installation may have multiple files similar to php.ini. Make sure you are modifying the php.ini file listed in phpinfo().

PHP has an error log you can configure in php.ini using the error_log directive. You can also configure the level of error reporting using the error_reporting directive.

Try PHP from the command line. This can be useful when you cannot get the SQL Anywhere extension to work inside your web server. It can make it easier to determine why it isn't working.

Related Content

Related Documents

Related SAP Notes/KBAs

 

 

  • No labels