What is the difference between longblob and longtext in mysql? - php

What is difference between longblob and longtext in mysql?
What should I use to save a long topic ?

BLOBs are for Binary Large Objects. If you're storing binary data in your DB, then BLOB would be the table type you want. Otherwise.. longtext.
In your case longtext.

BLOB and TEXT are basically identical, except that TEXT fields have character set translation rules applied. BLOB fields do not. So with BLOB what you put in is what you get. With TEXT, what you put may not be what you get out.

You can use collation & character sets on TEXT columns, which would mean that:
If you specify a different charset for a connection than the TEXT column is (for instance,latin1 column, utf-8 requested), MySQL will convert the contents to the required charset.
You can sort & compare TEXT columns based on collation.
BLOB's are just 'binary sequences', and you'll get them 'as is'.

Related

How to convert latin1 table to utf8 with serialized values?

I have to convert some huge tables (>60 GB) from latin1 to utf8 and I'm looking for the best practice. One problem is that some tables contain serialized php objects.
My first approach was to set the TEXT columns to BLOB, convert the character set to utf8 and convert the columns back to TEXT, but I got some issues with the last step (incorrect string value: '\xE4\xF6\xFC\xDF";...').
What would be the best strategy to convert the values properly to utf8?
Given that the data is in latin1 encoding, such as the äöüß in your example, and that the column is CHARACTER SET latin1, see http://mysql.rjweb.org/doc.php/charcoll#fixes_for_various_cases , which says
ALTER TABLE tbl CONVERT TO CHARACTER SET utf8mb4;
(or utf8)
Note: That will change the charset for all text columns in the one table; and only the one table.

MySQL saving UTF-8 entries from PHP correctly in VARCHAR but not in text blobs

I current have the following snippet of text in a text paragraph for my website
let’s get to it
The apostrophe character is part of the UTF-8 charset, and it saves properly in a table column that is designated a VARCHAR column, in the form
let’s get to it
Which is properly parsed by my client. If I put the same text into a TEXT column in MySQL, it's stored as the following:
let’s get to it.
Is there any reason the two would differ, and if so, how can I change it?
let’s is Mojibake. Latin1 is creeping in.
"text blob" -- which is it TEXT or BLOB? They are different datatypes.
let’s comes from htmlentities() or something equivalent. That can be stored and retrieved in VARCHAR, TEXT, or BLOB, regardless of CHARACTER SET. MySQL will not convert to that.
The Mojibake probably came from
The bytes you have in the client are correctly encoded in utf8 (good).
You connected with SET NAMES latin1 (or set_charset('latin1') or ...), probably by default. (It should have been utf8.)
The column in the tables may or may not have been CHARACTER SET utf8, but it should have been that.

Storing text cleanly into mysql TEXT field

Im attempting to store the following word in a TEXT field in my MYSQL db table:
Valentine’s
But for some reason it stores as the following:
Valentineâs
Is there a PHP function I can use to clean the string before i store it in the database..
the CHARSET of the table is latin1
htmlspecialchars(); is what you're looking for I guess.

Issue with charset and data

I try to explain the whole problem with my poor english:
I use to save data from my application (encoded on utf8) to database using the default connection of PHP (latin1) to the tables of my DB with latin1 as charset.
That wasn't a big problem : for example the string Magnüs was stored as Magnüs, and when I recovered the data I saw correctly the string Magnüs (because the default connection, latin1).
Now, I change the connection, using the correct charset, with mysql_query("SET NAMES 'utf8'", $mydb), and I've also changed the charset of my tables's fields, so the value now is correctly store as Magnüs on DB; Then I still seeing Magnüs when I retrieve the data and I print on my Web Application.
Of course, unfortunatly, some old values now are badly printed (Magnüs is printed as Magnüs).
What I'd like to do is "to convert" these old values with the real encoding.
ALTER TABLE <table_name> CONVERT TO CHARACTER SET utf8; will convert only the field type, not the data.
So, a solution (discovered on internet) should be this:
ALTER TABLE table CHANGE field field BLOB;
ALTER TABLE table CHANGE field field VARCHAR(255) CHARACTER SET utf8;
But these old string won't change on database, so neither in the Web Application when I print them.
Why? And what can I do?
Make sure that your forms are sending UTF-8 encoded text, and that the text in your table is also UTF-8 encoded.
According to the MySQL reference, the last two ALTER you mentioned do not change the column contents encoding, its more like a "reinterpretation" of the contents.
Warning
The CONVERT TO operation converts column values between the character sets. This is not what you want if you have a column in one character set (like latin1) but the stored values actually use some other, incompatible character set (like utf8). In this case, you have to do the following for each such column:
ALTER TABLE t1 CHANGE c1 c1 BLOB;
ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;
The reason this works is that there is no conversion when you convert to or from BLOB columns.

Why is only 64kB of data being saved in my MySQL data column?

I am trying to insert a very long text string into a MySQL Blob column, but MySQL is only saving 64kB of the data. The string is 75360 characters long. I am connecting with PHP's mysql_connect().
Any ideas?
Does it make a difference if it's Blob or Text. I originally had it as a Text but changed it with no affect.
Because that's the maximum size of a BLOB column. You need to use MEDIUMBLOB/LONGBLOB or MEDIUMTEXT/LONGTEXT.
A BLOB type in MySQL can store up to 65,534 bytes, if you try to store more than this much data MySQL will truncate the data. MEDIUMBLOB can store up to 16,777,213 bytes, and LONGBLOB can store up to 4,294,967,292 bytes.
If you enable strict SQL mode (MySQL modes) you will get an error when you try to store data that doesn't fit in the column type.
You also asked if there is a difference between BLOB and TEXT
BLOBS are for binary data. If you do a LIKE query on a BLOB field it will be case sensitive.
i.e.
SELECT 'TEXT' LIKE 'TEXT';
=> 1 for both BLOB and TEXT
SELECT 'TEXT' LIKE 'text';
=> 1 for TEXT
=> 0 for BLOB
The blob column is only 64Kb per the documentation
Try a mediumblob column type instead...

Categories