Insert base64 string into MySQL LONGBLOB. - php

I have a base64 (Image) string that I need to save on a MySQL data base as data type binary, LONGBLOB. The string is over 200,000 characters. The string is saved in a PHP variable.
I understand that this is not recommended, but I need some input on how it can be done. It has to be done in pure PHP. The base64 string is send through AJAX POST XMLHttpRequest.
Thanks.

Using LOAD_FILE might be what you are looking for:
UPDATE colum SET img=LOAD_FILE('/tmp/file') WHERE id=1;

Related

Best Way to Store XML in MySQL

I'm getting an XML API response, parsing the data I need, but want to store full XML response in MySQL for add'l data use later.
I intially stored the XML in a BLOB, but found special characters sometimes in values break the INSERT.
So, I first convert XML with htmlentities into a BLOB to keep original API response data integrity. Is this a good way to do it, or is there a better method?
Properly escaped, any data can be INSERTed into a BLOB. htmlentities is not the way. For the mysqli API, use mysqli::real_escape_string. For PDO use binding.
If you are running on Windows, be sure to fetch the data in binary mode, else CR/LF may get "fixed" during the read.
In theory XML should be UTF-8, so simply putting into TEXT CHARACTER SET utf8mb4 should work instead of BLOB.
If all else fails, consider converting (in the client) the blob into Base64. It will be 4/3 bigger.

I have trouble with Mysql Encoding

I'm using phpseclib for encrypting data, My MySQL database's encoding is utf8-general-ci.
when I encrypt an string and save it to the table some of characters appears in '?' charachter. This make mistake when I want to decrypt it.
What encoding should I use to have all of the characters?
please help.
If you encrypt your data to a binary string, it can no longer be stored in an UTF8 encoded string, since some binary values/sequences are just not valid UTF8.
Just base64 encode your string (or change the column type to a binary type) before storing it and things should work better.

Can json transfer longblob in php?

I used mysql to store the picture and it is stored by longblob.
Now I need use json the transfer the data of longblob.
The json_endcode($data) returns null.
How to do it?
You could try to serialize image data to base64, but it seems to be a bad idea, since images can be really big. You better store it on ftp server and write just links to images to database
Please check this question - Binary Data in JSON String. Something better than Base64
binary data can be encoded into base64 otherwise JSON does not support it
The JSON format natively doesn't support binary data. The binary data
has to be escaped so that it can be placed into a string element (i.e.
zero or more Unicode chars in double quotes using backslash escapes)
in JSON.

VARBINARY and BLOB in mysql for serialized data via PHP?

I am storing serialized data in a mysql and am unsure which field type to choose?
One example of the serialized data output is below,
string(393) "a:3:{s:4:"name";s:22:"PACMAN-Appstap.net.rar";s:8:"trackers";a:6:{i:0;s:30:"http://tracker.ccc.de/announce";i:1;s:42:"http://tracker.openbittorrent.com/announce";i:2;s:36:"http://tracker.publicbt.com/announce";i:3;s:23:"udp://tracker.ccc.se:80";i:4;s:35:"udp://tracker.openbittorrent.com:80";i:5;s:29:"udp://tracker.publicbt.com:80";}s:5:"files";a:1:{s:22:"PACMAN-Appstap.net.rar";i:4147632;}}"
The string lengths of the data can vary greatly upto around 20,000 characters.
I understand that I do not want to use TEXT data type as this could corrupt data because of character sets that it would have to use.
I am stuck as when it comes to use either VARBINARY, BLOB, MEDIUMBLOB etc.
Let us say if I use VARBINARY(20000) does this mean that I can insert a string of 20000 in length safely and if it is over then discard the insert?
I agree with PLB in that you should use BLOB. The length attribute specifies how many bytes can be saved in this column. The main difference between BLOB and VARBINARY is that VARBINARY fills up unused space with padding, wheras with BLOB only the actual length of the data is reserved for one field.
But as PLB said, only use this if you absolutely must, because it slows down the whole DB in most cases. A better solution would be to store the files in your server's filesystem and save the file's path in the DB.

MySQL - How to store AES_Encrypted data?

So I have been browsing the internet, and came across the MySQL built-in function AES_ENCRYPT. It doesn't seem too hard to use, but some sources tell me to store the encrypted data as a VARCHAR, and some say to store it as a BLOB. What should I store the encrypted data as?
Many encryption and compression functions return strings for which the result might contain arbitrary byte values. If you want to store these results, use a column with a VARBINARY or BLOB binary string data type. This will avoid potential problems with trailing space removal or character set conversion that would change data values, such as may occur if you use a nonbinary string data type (CHAR, VARCHAR, TEXT).
Source: http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html
If you need to use VARCHAR, rather than BLOB, then convert the encrypted binary to Base64 which only uses printable characters and can be safely stored as VARCHAR. Of course you will need to convert it back from Base64 to binary before decrypting.
I have always used blobs to stored encrypted data in MySQL.
You can use Binary. BINARY in STRING. It have to work. I am using it. Write me answer if it doesn't working.

Categories