[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 963
  • Last Modified:

PHP + MySQL - problem with BLOB


scripts use MySQL BLOB column to store binary files. I\m speaking about script download.php which should return stored data.

It worked for a long time without any problem. Code is below.

$sql = "SELECT file,filename FROM orders WHERE id = $id";
$result = mysql_query($sql);

$filename = mysql_result($result,0,'filename');
$file = mysql_result($result,0,'file');

Last week we've got a problem. Now variable $file doesn't contains entire BLOB, but only first 8 bytes. I've tried to use  mysql_fetch_row - the same result.

Have anyone seen this before?
  • 4
  • 4
1 Solution
Richard QuadlingSenior Software DeveloperCommented:
Has the data been truncated on the way into the database?

Instead of returning the data, can you try getting the length of the field in the SQL statement instead?

There is a LENGTH() function on MySQL, so ...

SELECT LENGTH(file),filename FROM orders WHERE id = $id

should at least tell you if the data is OK.

andreifAuthor Commented:
When I use MySQL fron I can see stored data.

select * from orders where id=1
select id,file from orders where id=1
return complete blob

select file from orders where id=1
returns truncated

All of them returns 8 chars when called from PHP
I repeat, this works last week :(
Thisat script works on another server as well. I suggest, that this is the question of server configuration (may be load)?
andreifAuthor Commented:
length(file) returns 32256 - so it should be okay
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Sounds weird.

It looks like you only want one blob column from one row, so a possible workaround is to ...
  select file from orders where id=1 INTO DUMPFILE "/tmp/file.ext"

... and then read then file.

What type of data is it?  Text, image,...?
... and then read the file.
What version of MySQL are you using?  Perhaps an upgrade might make the problem go away.
andreifAuthor Commented:
Data type is binary - it can be .doc, .pdf or something else.
MySQL version is 3.23.49

I'll try DUMPFILE, but anyway, the problem is very interesting I wonder what can be the reason of this strange behaviour.
What are the differences between the server that it works on and the one where it doesn't?

Is it the same version of MySQL?

Are they on the same O/S, version?

Are they the same Web Server, version?

Is it the same version of PHP?

andreifAuthor Commented:
OS, MySQL & PHP versions are different, but unfortunately, I can't change this.

Most interesting is the fact that script worked before and administrator says that he haven't changed anything in server configuration last days.

I've spent some time looking for answer in manuals & forums and should say that found nothing. I've seen some mentions about buffer size, but as I see, in my case buffer is big enough.

Anyway, that solution with dumpfile works and thanks to Hamlet for quick & working idea!

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now