UTF-8 Cyrillic error - edited - mysql_real_escape_string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

UTF-8 Cyrillic error - edited - mysql_real_escape_string

Post 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?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post 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).
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Reply

Post 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?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Try setting your connection encoding with SET NAMES 'utf-8'
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Rely

Post 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?
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Reply

Post by user___ »

Guys, I have fixed it.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

To help out others, it would be nice if you posted the solution you found.
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Reply

Post 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.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: Reply

Post 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.
Post Reply