SQL Anywhere databases can store any type of data as a binary large object (BLOB). If that data is of a type readable by a web browser, a PHP script can easily retrieve it from the database and display it on a dynamically generated page.
BLOB fields are often used for storing non-text data, such as images in GIF or JPG format. Numerous types of data can be passed to a web browser without any need for third-party software or data type conversion. The following sample illustrates the process of adding an image to the database and then retrieving it again to be displayed in a web browser.
This sample is similar to the sample code in the files image_insert.php and image_retrieve.php of your SQL Anywhere installation. These samples also illustrate the use of a BLOB column for storing images.
<?php $conn = sasql_connect( "UID=DBA;PWD=sql" ) or die("Cannot connect to database"); $create_table = "CREATE TABLE images (ID INTEGER PRIMARY KEY, img IMAGE)"; sasql_query( $conn, $create_table); $insert = "INSERT INTO images VALUES (99, xp_read_file('ianywhere_logo.gif'))"; sasql_query( $conn, $insert ); $query = "SELECT img FROM images WHERE ID = 99"; $result = sasql_query($conn, $query); $data = sasql_fetch_row($result); $img = $data; header("Content-type: image/gif"); echo $img; sasql_disconnect($conn); ?>
To be able to send the binary data from the database directly to a web browser, the script must set the data's MIME type using the header function. In this case, the browser is told to expect a GIF image so it can display it correctly.
Discuss this page in DocCommentXchange.
|Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1|