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

ZIGLIO, Frediano, VF-IT Frediano.Ziglio at vodafone.com
Thu Apr 6 04:25:35 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
> 

This is che default setting, use SET TEXTSIZE xx where xx is the limit
you want (0 for no limit that is 2GB)

> 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"));
> 

I would try prepared statement, it do not need to quote image.

> $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);
> }

freddy77


More information about the FreeTDS mailing list