Re: PHP & MS Sql WRITETEXT inserts

From: Robert (hackajar_at_yahoo.com)
Date: 04/05/04


Date: 5 Apr 2004 13:15:29 -0700

And here's the answer - (after digging in multi-sources)

//Open File we will be importing
$file = "./images/myimage.png";
$fp = fopen($file, 'r');
$raw = fread($fp, filesize($file));

//Convert to hex string
$data = unpack("H*hex", $raw);
$buf = $data['hex'];

//Set textsize to max limit, allows large files to be imported
$query = "set textsize 2147483647";
mssql_query($query) or die();

//Push data up to database (note the '0x' before data)
$query = "INSERT INTO $database.dbo.image_data (iid, idata) values
('$iname', 0x$buf)";
mssql_query($query);

MsSql will know to sent down bianry when you select the data. Hope
this helps others like me!

Robert

hackajar@yahoo.com (Robert) wrote in message news:<895be58e.0404011725.7e8a19a9@posting.google.com>...
> Anyone know a clever way of getting binary data into a MS Sql database
> with PHP?
>
> My code that fails:
>
> --->Snip
> $query = "DECLARE @mytextptr varbinary(16)
> SELECT @mytextptr=TEXTPTR(idata)
> FROM $database.dbo.image_main
> WHERE iid='$iname'
> IF @mytextptr IS NOT NULL
> WRITETEXT apig_image_main.idata @mytextptr WITH LOG '$buf'";
>
> $result = mssql_query($query) or die();
> <---Snip
>
> Error messages:
> Warning: mssql_query(): message: Unclosed quotation mark before the
> character string '?PNG '. (severity 15) in c:\program files\apache
> group\apache\htdocs\image.php on line 58
>
> Warning: mssql_query(): message: Data stream missing from WRITETEXT
> statement. (severity 15) in c:\program files\apache
> group\apache\htdocs\image.php on line 58



Relevant Pages

  • Re: text to number format or preceeding 0
    ... justified or preceed with zeros. ... I am importing a raw txt file into a dbase ...
    (microsoft.public.access.queries)
  • Content question
    ... I am setting up a SQL database which could end up with 150 million records. ... There are two options storing large text files (which may include raw image ... instead of the raw data. ...
    (microsoft.public.sqlserver.server)
  • Re: Get date of a file
    ... I'm importing the contents of a csv file into a SQL database but ... Rick ...
    (microsoft.public.vb.general.discussion)
  • Re: Hide/Protect Tables from imports?
    ... if you need strong security use a SQL database and not ... move them to a 2nd backend file and used Windows file ... importing those tables. ...
    (comp.databases.ms-access)
  • Re: Get date of a file
    ... BCS wrote: ... I'm importing the contents of a csv file into a SQL database but the ...
    (microsoft.public.vb.general.discussion)