Storing and retrieving image MSSQL via php

Hi All,

I have the following code that essentially stores and retrieves a file from a MSSQL db. The jpeg that is being stored in the db column is of the correct type e.g. IMAGE (mysql eqv of BLOB). Code Below:

For Storing:


$datastring = file_get_contents("someimage.JPG");
$data         = unpack("H*hex", $datastring);
mssql_query("insert into sometable values ('someimage.JPG', 0x".$data['hex'].")");

For Retrieving:


$result = mssql_query("select someimages from sometables where somename like 'someimage'");
$row = mssql_fetch_row($result);
header("Content-type:image/jpeg;");
echo $row[0];

The problem occurs when I try to retrieve the image, i get the following error from the browser (not php).

The image “someimage” cannot be displayed, because it contains errors.

I can only assume the hex becomes corrupted, any help would be greatly appreciated.

Thank in adavnce D

I figured this out, for anyone that is experiencing the same problem here was my solutions
make sure the hex/binary data you insert into the mssql image column is unquoted, see mssql docs about image types for more info. Probally the most important part use

mssql_result($result, 0, 0) instead of just mssql_result($result)