alexb
alexb

Reputation: 470

Inserting a custom binary format blob into SQL

I have a log file which was made in a custom binary format. This log file is uploaded to a server and PHP inserts it into a MySQL database, like so

$tmpName = $_FILES['file']['tmp_name'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$query = "INSERT INTO device_log (dID, deviceLog) VALUES ( '$res1', '$content')";

But this doesn't always work, because SQL has trouble with its contents, sometimes giving a You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '««»»Qì™úX' at line 1 error.

I've read that you need to escape the data with something like mysql_real_escape_string, but this would ruin the initial data, i.e. I wouldn't be able to read it back. What is the correct way to insert such a blob?

Upvotes: 0

Views: 193

Answers (1)

Bob Arezina
Bob Arezina

Reputation: 84

You might want to base64 encode the data before insertion into your database, that might alleviate the issue for you (you need to base64 decode your data when reading it back).

Upvotes: 1

Related Questions