PEAR DB and BLOB Fields in MySQL
| From: | Russell Seymour | Date: | Fri, 14 Mar 2003 09:35:59 +0000 |
| Subject: | PEAR DB and BLOB Fields in MySQL | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-14283@lists.php.net to get a copy of this message | ||
Good Morning list,
I am currently using the PEAR:DB functions to access my MySQL database and
up until now I have no problems in using it. However recently I have
started to update an internal intranet and I need to be able to retrieve
images that are stored in a BLOB field in a MySQL database.
The reason that I am asking for help on the list is that I can get the image
to display correctly if I use the standard PHP MySQL functions, but it does
not work when I convert to PEAR:DB. If anyone has any ideas I would be most
grateful.
If I use PEAR:DB I get both the binary data back and the content type, but
the problem is the image is displayed as a cross indicating that the image
has not been sent to the browser correctly, this does not happened with the
PHP mysql functions.
The structure of the table in the database is as follows:
CREATE TABLE images_TABLE (
id int(4) unsigned not null,
image longblob not null,
imagetype varchar(50),
primart key (id), index (id)
);
Thanks very much in advance. Examples of both sets of code is at the end.
Regards,
Russell Seymour
METHOD 1 - PHP Built in MySQL Functions ***********************************
<?php
// Define SQL Statement
$s_SQL = "SELECT image, imagetype FROM images_TABLE WHERE id = 8";
// Connect to the database
mysql_connect (DBSERVER, DBUSER, DBPASSWD);
// Select the database
mysql_select_db ("images");
// Run the query on the database
$o_Image = mysql_query ($s_SQL);
// Get the relevant binary data from the data set
$binary_Image = mysql_result($o_Image, 0, "image");
$content_Type = mysql_result($o_Image, 0, "imagetype");
Header ("Content-type: $content_Type");
echo $binary_Image;
?>
METHOD 2 - PEAR:DB Functions *******************************************
<?php
// Include and instantiate classes
include_once ("DB.php");
$o_DB = DB::Connect ("mysql://DBUSER:DBPASSWD@DBHOST/image");
// Define SQL Statement
$s_SQL = "SELECT image, imagetype FROM images_TABLE WHERE id = '8'";
// Run the query on the database
$o_Image = $o_DB -> query ($s_SQL);
// Get the binary data from the resultset
list ($binary_Image, $content_Type) = $o_Image -> fetchrow();
// Output the data to the browser
Header ("Content-type: $content_Type");
echo $binary_Image;
?>