Forum Moderators: coopster

Message Too Old, No Replies

Help for encoding and decoding string

Before MYSQL insertion

         

tomda

12:46 pm on Aug 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I am having a very strange problem and I am sure someone will be able to assist me.

Simple query as an example :

INSERT INTO category (`id`, `title`) VALUES (NULL,'é\"\')ç(_)(_)è_()\"è()\"')

When processing the query on my website, I get this in my table.

é"')ç(_)(_)è_()"è()"Ã

But when I open PHPMYADMIN and type EXACTLY the same query, it works great.

And when I create a test.php file and run the same query, it works fine also.

Psychopsia

2:22 pm on Aug 31, 2006 (gmt 0)

10+ Year Member



You should try with this function:

$str = 'é"\')ç(_)(_)è_()"è()"';
$str = htmlentities($str);

To save in database use it too:

$str = mysql_escape_string($str);

tomda

6:07 am on Sep 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks PSychopsia,

This is what I have done temporarly but I don't like inserting HTML entities into my database because there are two issues.

Field varchar limit
*******************
As for example, if I limit my title to 100 characters in my database and someone type a text with lot of special characters then the title is trimmed during insertion.

Uppercase
*********
If I want to print my title in uppercase - like strtoupper($title) - then é become É instead of É. Meaning that I need to decode the title, change it to uppercase and encode it again using htmlentities().

Am I wrong when I say that putting HTML entities in the database is not recommanded?

Anyway, I know almost the functions that may used (htmlspecialcharacter, htmlentities, mysql_escape_string, shellcmd, addslahes, magic_quote, etc.)

I 'll try to see what the problem... I suspect there is problem with charset, though both my database and my website are in UTF-8.

tomda

8:39 am on Sep 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep, found it...

As I said, the problem was due to the Collation Unicode Encoding - though both my website and SQL were in UTF-8.

Setting UTF8 in the database by _utf8 in the INSERT statement works

INSERT INTO $tcat (`id`, `title`) VALUES (NULL, _utf8'".$form_title."')";

For advanced reading about Collation Unicode Encoding and UTF-8, go to
[dev.mysql.com...]