[freetds] php + tds with sql server, retrieving images

Martin Rode martin.rode at programmfabrik.de
Wed Apr 5 11:00:25 EDT 2006


hi everybody,

i have

* Debian Linux
* PHP 4.4.2-1+b1
* tdsodbc        0.63-2
* unixodbc       2.2.11-12

My problem is, that via ODBC i can write image binary to the database 
(using the 0x notation), but I can only retrieve 4096 bytes from that 
binary back. What am I doing wrong?

Thanks for your support!
Martin

PHP script comes here:
------------------------------------------------------------------------------------------

$dsn ="DRIVER={FreeTDS};SERVER=thepope;DATABASE=test";
$link2 = odbc_connect($dsn , 'sa', 'test' ) or die(odbc_errormsg() );

$datastring = file_get_contents("/home/martin/jpegs/goil.jpg");

odbc_autocommit($link2, true);

odbc_exec($link2, "IF EXISTS(SELECT TABLE_NAME FROM 
INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'images') DROP TABLE 
[images];");


odbc_exec($link2, "
 CREATE TABLE images (
    name varchar(50),
    data image
);");

$data = 
odbc_escape_string(file_get_contents("/home/martin/jpegs/goil.jpg"));

$sql = "insert into images (name, data) values ('img.jpg',$data);";
odbc_exec($link2,$sql);

// retrieving: this only gives me 4096 bytes
$result = odbc_exec($link2, "select data from images;");
header("Content-Type: image/jpg");
odbc_longreadlen($result, 8192);
$row = odbc_fetch_array($result);
echo $row['data'];


function odbc_escape_string($datastring) {
    $data = unpack("H*hex", $datastring);    
    return '0x'.substr($data[hex],0,strlen($data[hex])-1);
}
-----------------------------------------------------------------




More information about the FreeTDS mailing list