Sybase Business Intelligence Solutions - Database Management, Data Warehousing Software, Mobile Enterprise Applications and Messaging
Sybase Brand Color Bar
delete

Search for    in all of Sybase.com
view all search results right arrow
  blank
 
 
 
 
 
 
 
 
 
 

 
 
CLICK TO EXPAND NAVIGATION
CLICK TO EXPAND NAVIGATION
 
 
 
 
Support > Technical Documents > Document Types > Technote > Introduction to the PHP External Environment

Introduction to the PHP External Environment
 
RSS Feed
 
 
 

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.

Screenshot of creating sp_root

CREATE SERVICE ROOT
TYPE
'RAW'
USER DBA
AUTHORIZATION OFF
URL ON
AS CALL
sp_root(:URL)

Creating the procedure

The sp_root() procedure will set the HTTP header and output the string 'test'.

CREATE PROCEDURE sp_root( IN URL LONG VARCHAR )
BEGIN
   CALL
sa_set_http_header('Content-Type','text/html');
   SELECT 'test';
END

Reading code from a file

The procedure can be altered to read from a file based upon the input URL.

ALTER PROCEDURE sp_root( IN URL LONG VARCHAR )
BEGIN
   CALL
sa_set_http_header('Content-Type','text/html');
   SELECT xp_read_file(URL);
END

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:

<?php
   echo phpinfo();
?>

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.

Screenshot of phpinfo source

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:

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

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.

Screenshot of phpinfo

Note:

It is also possible to parse php scripts from within the database. sa_http_php_page() requires a LONG VARCHAR parameter.

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.

 
 
 

 
© Copyright 2014, Sybase Inc. - v 7.6 Home / Contact Us / Help / Jobs / Legal / Privacy / Code of Ethics