I am using a LONGBLOG
field in a hosted MySQL database for uploading & downloading attachments. I used the example code found
The BINARY and VARBINARY Types
The BINARY
and VARBINARY
types are similar to CHAR
and VARCHAR
, except that they store binary strings rather than nonbinary strings. That is, they store byte strings rather than character strings. This means they have the binary
character set and collation, and comparison and sorting are based on the numeric values of the bytes in the values.
The permissible maximum length is the same for BINARY
and VARBINARY
as it is for CHAR
and VARCHAR
, except that the length for BINARY
and VARBINARY
is measured in bytes rather than characters.
The BINARY
and VARBINARY
data types are distinct from the CHAR BINARY
and VARCHAR BINARY
data types. For the latter types, the BINARY
attribute does not cause the column to be treated as a binary string column. Instead, it causes the binary collation for the column character set (or the table default character set if no column character set is specified) to be used, and the column itself stores nonbinary character strings rather than binary byte strings. This differs from BINARY(5)
, which stores 5-byte binary strings that have the binary
character set and collation. For information about the differences between the binary
collation of the binary
character set and the _bin
collations of nonbinary character sets.
If strict SQL mode is not enabled and you assign a value to a BINARY
or VARBINARY
column that exceeds the column’s maximum length, the value is truncated to fit and a warning is generated. For cases of truncation, to cause an error to occur (rather than a warning).
When BINARY
values are stored, they are right-padded with the pad value to the specified length. The pad value is 0x00
(the zero byte). Values are right-padded with 0x00
for inserts, and no trailing bytes are removed for retrievals. All bytes are significant in comparisons, including ORDER BY
and DISTINCT
operations. 0x00
and space differ in comparisons, with 0x00
sorting before space.
For VARBINARY
, there is no padding for inserts and no bytes are stripped for retrievals. All bytes are significant in comparisons, including ORDER BY
and DISTINCT
operations. 0x00
and space differ in comparisons, with 0x00
sorting before space.
For those cases where trailing pad bytes are stripped or comparisons ignore them, if a column has an index that requires unique values, inserting values into the column that differ only in number of trailing pad bytes results in a duplicate-key error. For example, if a table contains 'a'
, an attempt to store 'a\0'
causes a duplicate-key error. And have uploaded files into the database (although not able to check each byte in the file).
I got problems when downloading these files: txt files are OK, but binary files such as JPEG or PDF files always have error. The bytes in these binary files have been changed either during uploading or downloading, or during both.
I thought it might be a collation issue, but cannot fix it.