Martin M.
Martin M.

Reputation: 91

Can I insert binary data into MySQL TEXT column?

Is it possible to insert binary data into MySQL TEXT column? I have a table that I'd like to use for storing both UTF-8 text and binary data (very basic structure - id int(11), type char(1), data text), but when I try to insert some binary data (JPEG image) into the data column, the column is empty.

I use mysql_query and mysql_real_escape_string in PHP to execute the INSERT query, both of them should be binary safe. Example code:

mysql_query("INSERT INTO my_table VALUES(" . (int)$id . ", 'b', '" . mysql_real_escape_string($jpegImageData) . "')");

I don't want to change the data column type to BLOB - in some cases, I need to use to compare / collate strings in this column.

Upvotes: 5

Views: 2538

Answers (1)

Vlad Ogay
Vlad Ogay

Reputation: 135

Why not? Just encode in base64 and put it into that field.

<?php
$str = 'Some UTF-8 string or even a binary array';
echo base64_encode($str);
?>

Upvotes: 1

Related Questions