note 31688 added to function.mssql-query
| From: | arnarb at oddi dot is | Date: | Wed, 30 Apr 2003 11:16:24 +0000 |
| Subject: | note 31688 added to function.mssql-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-47287@lists.php.net to get a copy of this message | ||
If you'd like to store binary data, such as an image, in MSSQL, it's common to have
problems with addslashes and co.
This is because the MSSQL parser makes a clear distinction between binary an character constants.
You can therefore not easilly insert binary data with "column = '$data'" syntax
like in MySQL and others.
The MSSQL documentation states that binary constants should be represented by their unquoted
hexadecimal byte-string. That is.. to set the binary column "col" to contain the bytes
0x12, 0x65 and 0x35 you shold do "col = 0x126535" in you query.
I've successfully stored and retrieved jpeg images in a column with the "image"
datatype. Here's how:
// storing a file
$datastring = file_get_contents("img.jpg");
$data = unpack("H*hex", $datastring);
mssql_query("insert into images (name, data)
values ('img.jpg', 0x".$data['hex'].")");
// retrieving
$result = mssql_query("select data from images where name = 'img.jpg'");
$row = mssql_fetch_assoc($result);
header("Content-type: image/jpeg;");
echo $row['data'];
As you can see there is nothing to do with the image on they way out, just blurb out the buffer your
recieve as with any other field type.
--
http://www.php.net/manual/en/function.mssql-query.php
http://master.php.net/manage/user-notes.php?action=edit+31688
http://master.php.net/manage/user-notes.php?action=delete+31688
http://master.php.net/manage/user-notes.php?action=reject+31688