Search the Community
Showing results for tags 'binary mysql'.
-
Hello all I have a tricky problem that I have been wrestling with for a few days now, but cannot seem to make any headway. I'm not sure if it is an AutoIT related issue, or a mySQL issue, so I will try here first. The Mission Read in a file, store it in a database blob field, retrieve said file from database, and present to user. Files will generally be Word or Excel or jpgs. So, to some code. I'm going to leave out the boring stuff like starting mySQL, var declarations and the like, to get to the nub of the matter. global $chars,$sSQL,$name="test.xlsx",$type="Excel",$iRval,$hDBHandle,$aResult global $file = FileOpen($name, 16) global $size = FileGetSize($name) So here we just open the file in binary mode, and get the size. The size reported of the file above is 10253 bytes, which corresponds to the Windows directory listing of 11k. All good so far. Next, I read the entire file into a variable and then close the input file: $chars = FileRead($file,$size) FileClose($file) Just to check the integrity of the contents of the $chars variable, I write the whole thing back out to another file (this is just for testing purposes, to get my head around what is happening): $file = FileOpen("testoutput.xlsx",2) Filewrite($file,$chars) FileClose($file) I think do a checksum on both the input and output files. They are identical as you would expect. Now comes the part where it starts to go pear shaped. I write the $chars out to the database. (The "content" field below is defined as a mediumblob.) $sSQL = "INSERT INTO `test`.`upload2` (`name`, `type`, `size`, `content` ) " _ & "VALUES (""" & $name & """, """ _ & $type & """, """ & $size & """, """ & binary($chars) & """);" $iRval = _SQL_Execute($hDBHandle,$sSQL) if $iRval = $SQL_ERROR Then msgbox(0,"Error","Error inserting row !") Else msgbox(0,"Success","Record saved") EndIf The record saves fine, and a quick peek into the database shows the blob is stored in the "content" field. (BTW, the "binary" function in the above sql statement was just a test. It made no difference using it or not.) Now at this stage I can either retrieve the same row using SQL or go directly to the database using MySQL Workbench and retrieve the blob data. It doesn't matter which I use the result is the same. The result is that the blob is now exactly twice the size of the input ($chars) plus two bytes. And I have no idea why. And it is driving me crazy. I am pretty sure mySQL is not at fault, as I can manually input a file into the blob field above, and then retrieve it, and input and output will be identical. I'm at a road block on this and need some help kind people. I would even look at an alternative solution. Something whereby users can drop attachments into my application, and then (other users) can view them at a later stage. Thanks in advance Clark