Page 1 of 1
UTF-8 Cyrillic error - edited - mysql_real_escape_string
Posted: Sun Apr 29, 2007 12:25 pm
by user___
Hi guys,
I have a simple HTML form and a MySQL db whose charset is utf-8 and collatio is utf_general_ci. My HTMK encoding is UTF-8(As it is said to be better than windows-1251). I send some text in Russian and I get an error. When I send send the same text with windows-1251 encoding of my web page it works.
The error I get is: 'Data too long for column 'answer' at row 1'.
Any ideas.
I do need to make it work with UTF-8 not with windows-1251.
I found where the problem comes from but another question raised. So the problem comes from mysql_real_escape_string(). When I remove it my code works as it is supposed to but else it does not.
Do you know why?
Posted: Sun Apr 29, 2007 1:25 pm
by kaszu
I strongly suggest to use mysql_real_escape_string().
Problem is that in database you have 'varchar' or 'char' type for column 'answer' with some size and you are trying to insert more data than column size.
To solve this You can increase size of the 'answer' column (still will have this problem with more data than expected) or change it to type 'text' (which i think is the better solution).
Reply
Posted: Sun Apr 29, 2007 1:51 pm
by user___
Thank you for your reply.
I totally agree with you about mysql_real_escape_string() but although I did what you had suggested I got the same error.
Do you have any other ideas?
Posted: Sun Apr 29, 2007 6:21 pm
by Ambush Commander
Try setting your connection encoding with SET NAMES 'utf-8'
Rely
Posted: Mon Apr 30, 2007 5:11 am
by user___
Ambush Commander: I do so with the only exception that I set it with utf8 but not with utf-8. I do so because I have read in mysql.com that I have to do it in that way. When I use utf-8 my insets and updates work but anything selected is '?'.
When I set the column type to blob it works but when I try to open it it seems that nothing has been saved.
It does not work with anything else.
Do you have any ideas?
Reply
Posted: Mon Apr 30, 2007 5:41 am
by user___
Guys, I have fixed it.
Posted: Mon Apr 30, 2007 1:46 pm
by Ambush Commander
To help out others, it would be nice if you posted the solution you found.
Reply
Posted: Mon Apr 30, 2007 2:40 pm
by user___
Well, it is quite simple. I put the connection var in mysql_real_escape_string($str, $connection_var)l and it started working as it is supposed to. I use utf8 encoding, as I have already said.
BTW: After I fixed this I got another problem of the same kind but it was not with mysql_real_escape_string() but with htmlentities($str);. I set it in this way without the other parameters and I got an error. I fixed it by passing them.
I recommend anybody not to undertake parameters in parenthesis with Php functions.
Re: Reply
Posted: Tue May 01, 2007 12:16 pm
by jmut
user___ wrote:Well, it is quite simple. I put the connection var in mysql_real_escape_string($str, $connection_var)l and it started working as it is supposed to. I use utf8 encoding, as I have already said.
BTW: After I fixed this I got another problem of the same kind but it was not with mysql_real_escape_string() but with htmlentities($str);. I set it in this way without the other parameters and I got an error. I fixed it by passing them.
I recommend anybody not to undertake parameters in parenthesis with Php functions.
Glad you made it.
By the way if you have only one connection and mysql_real_escape_string does not get the correct encoding...without explicitly passing $connection_var I think this should be considered a php bug. As stated in PHP manual...if not passed ...last connection opened with mysql_query will be used...apperantly in your case it was used...but encoding lost somehow.