Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to view MS SQL BLOB field (picture) using PHP 1

Status
Not open for further replies.

123jon

Programmer
Jan 11, 2003
14
US
I am unable to view pictures (jpeg) from a MSSQL Server 2005 database (BLOB field), no image is displayed on the web page (red X displayed).

Also as a troubleshooting step, I displayed the content of the BLOB field and got some characters which I dont understand.

Picture type: JPEG
PHP version: 5.3.0
OS: Windows XP
Browser: Internet Explorer (IE) 6
MS SQL Server version: 2005
I am connecting to the MS SQL server through an ODBC driver (SQL Server)

Can someone please help? See the PHP code below...

------------------------
<?php
// Calls the GETPIC script
$s="getpic.php";
echo "<img src='".$s."'>";
?>

--------------------------
<?php
// GETPIC script

$picid='252';

/* MS SQL Server */
$connectionMSSQL = odbc_connect("Driver={SQL Server};Server=$myServer;Database=$myDatabase", $myUser, $myPass) or die ("Unable to connect");

if ($connectionMSSQL) {
$result = odbc_exec($connectionMSSQL, "SELECT MMOBJS.LNL_BLOB AS IMAGE FROM EMP, UDFEMP, TITLE, CITYDB, MMOBJS WHERE EMP.ID='".$picid."' AND EMP.ID=UDFEMP.ID AND UDFEMP.TITLE=TITLE.ID AND CITYDB.ID=UDFEMP.CITYDB AND EMP.ID=MMOBJS.EMPID AND MMOBJS.OBJECT=1 AND MMOBJS.TYPE=0 ORDER BY EMP.LASTNAME, EMP.FIRSTNAME, EMP.ID");

} else {
die("Error");
}

while ($row = odbc_fetch_array($result)) {
// Declare variables and assign values
$cMyIMAGE=$row['IMAGE'];
break;
}
// display picture
header("Content-Type:image/jpeg");
echo $cMyIMAGE;
?>
-----------------

ALSO: This is the first part of what's displayed (echo) when I view the contents of the BLOB field in IE (I am not using the header command here, just viewing the BLOB contents):

ÿØÿàJFIF––ÿþ"LENEL OnGuard Chromakey=24,30,30ÿÛ„    $.' "+"(6(+/1343&8<82<.231  1!!11111111111111111111111111111111111111111111111111ÿÄ¢  }!1AQa"q2?‘¡#B±ÁRÑð$3br‚ %&'()
 
I see nothing wrong with the blob. It seems to be a JPEG image. Is the GETPIC script in the right directory and in the right character case? Are you still getting getting the error if you make this script echo the contents of an existing image?

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
the output looks a bit small for a binary jpeg.

there are often reported problems extracting large amounts of data from mssql with odbc connections. from recollection it is something about the buffer and block sizes that you have to set. it is a {bug} with odbc and not with php.
 
Thanks DonQuichote and Jpadie for your response. Data was being dropped, as suggested by Jpadie, so I used the following command before fetching the data:

ini_set("odbc.defaultlrl","99999999");

Issue resolved. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top