ash1337's avatar

Blob - SQLSTATE[22001]: String data, right truncated

Hi All,

any help on this topic would be great. Cant seem to figure out why I am getting the 22001 error when saving an encoded file in a blob. What is odd is that it is seems to be random, most times it works fine others it fails.

error:

SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'label' at row 1

my code:

$orderPackage->label = $response['labelData'];
$orderPackage->save();

I am able to confirm that my response variable is correctly populated.

in my migration code:

$table->binary('label')->nullable();

this is creating a blob field in mysql

I am getting a PDF response in base64 encoded and storing it in a blob. I know it is normally not recommended to store files in DB, but in my use case I need to.

What has got me scratching my head is that 80-90% of time, it works.. sometimes it does not.

any insights would be great.

0 likes
4 replies
Tray2's avatar
Tray2
Best Answer
Level 74

I would suggest that you don't store binary data inside your database, that is not what it is for. It's much better to store the binaries on the filesystem, and then just add the path to the table.

However, it is likely creating a regular blob, which is 65535 bytes, so you need to use a larger version of the blob.

https://www.blazemeter.com/blog/blob-mysql

ash1337's avatar

Looks like that was the problem.. Laravel by default creates just the standard blob field.. that kept my head spinning.. looks like this is just a mysql thing which is probably why there isnt a native way to tweak the table.

Totally agree with not storing files in DB -- but as I stated, I kind of dont have a choice in this use case.

Please or to participate in this conversation.