This document will explain how to use SQL Anywhere's built-in http server in conjunction with the PHP external environment module.
Create a database
Initialize a database in the directory of your choice. Use Sybase Central or from the command prompt:
dbinit test.db
Start the http server
From the command prompt, start the server with the -xs switch and specify the port:
dbeng11 test.db -xs http(port=8000)
Create a root web service
First, a web service needs to be created to define how the SQL Anywhere http server should process incoming URLs. This service will interpret the remainder of the URL as a value and pass it as a variable to the sp_root() procedure (the sp_root() procedure will be created in the next step). For more information on creating a web service, see the documentation.
Creating the procedure The sp_root() procedure will set the HTTP header and output the string 'test'. Reading code from a file The procedure can be altered to read from a file based upon the input URL. Now, the server will read and output the text of the file specified by the user in the URL. For example, if a user types in a browser: http://localhost:8000/phpinfo.php, SQL Anywhere will read and output the text found in the file phpinfo.php located in the same directory as the database. Since, phpinfo.php does not currently exist, nothing will be displayed. Creating a script Go ahead and create the phpinfo.php file in the same directory as the database. The file will contain a simple php script: Browsing to http://localhost:8000/phpinfo.php returns a blank screen. However, viewing the page source reveals that it contains the contents of phpinfo.php.
Using the PHP External environment The final change to the sp_root() procedure will make use of the PHP external environment module to parse the php script: Using the sa_http_php_page system procedure, the text (php code) that is returned from xp_read_file() will be interpreted by PHP in a separate process.
Note: It is also possible to parse php scripts from within the database. sa_http_php_page() requires a LONG VARCHAR parameter. CREATE SERVICE ROOT
TYPE 'RAW'
USER DBA
AUTHORIZATION OFF
URL ON
AS CALL sp_root(:URL) CREATE PROCEDURE sp_root( IN URL LONG VARCHAR )
BEGIN
CALL sa_set_http_header('Content-Type','text/html');
SELECT 'test';
ENDALTER PROCEDURE sp_root( IN URL LONG VARCHAR )
BEGIN
CALL sa_set_http_header('Content-Type','text/html');
SELECT xp_read_file(URL);
END
ALTER PROCEDURE sp_root( IN URL LONG VARCHAR )
BEGIN
CALL sa_set_http_header('Content-Type','text/html');
SELECT sa_http_php_page(xp_read_file(URL));
END
Copyright 1994-2009 iAnywhere Solutions, Inc. All rights reserved. This sample code is provided AS IS, without warranty or liability of any kind.
You may use, reproduce, modify and distribute this sample code without limitation, on the condition that you retain the foregoing copyright notice and disclaimer as to the original iAnywhere code.

Back to Top