Forum Moderators: coopster & phranque

Message Too Old, No Replies

Greek letters (Numeric Character References) in MySQL?

         

Trisha

12:41 am on Nov 7, 2002 (gmt 0)

10+ Year Member



I created a PHP/MySQL CMS thingy for a site and I'm having problems with using Greek characters with it. It isn't easy to explain what happens, but I'll try by going through it one step at a time.

When I first add a Greek character in, within the HTML form I set up for editing the site, I write it as a decimal character reference - for example:


α

and then check it on the live page, and it seems ok,

But if I go back to edit the article again, it actually appears as the Greek letter (in this case a small alpha) within the HTML form.

If I save changes to the article and then check the live page once again, the character has been changed to something other than an alpha character. (In my case a box, but I believe that is part of another issue, many characters are not displaying correctly for me, likely an OS thing). The source code then shows it as an letter "a" with an accent mark on it, rather than the character reference:


α

which I had originally typed in.

Is this some kind of inherent problem with MySQL? Is there anything I can do about it? As it is, if I make any changes to existing articles I will have to remember if I used any non standard characters, and change them back to the correct decimal character reference before saving again.

andreasfriedrich

1:36 am on Nov 7, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Explanation

As it really isn't easy to explain what happens letīs try by going through it one step at a time.

When I first add a Greek character in, within the HTML form I set up for editing the site, I write it as a decimal character reference - for example:

α

The browser will treat that as the literal characters &, #, 9, 4, 5, ;. Character reference are only turned into their real character when the browser loads the page.

and then check it on the live page, and it seems ok,

The browser now sees the character reference and changes it into the real character.

But if I go back to edit the article again, it actually appears as the Greek letter (in this case a small alpha) within the HTML form.

This is basically the same as the previous step. The character reference is turned into the real character. If you submit the form you get the real character.

If I save changes to the article and then check the live page once again, the character has been changed to something other than an alpha character. (In my case a box, but I believe that is part of another issue, many characters are not displaying correctly for me, likely an OS thing). The source code then shows it as an letter "a" with an accent mark on it, rather than the character reference:

α

which I had originally typed in.

The real character now gets send to the server and gets garbled if you use the wrong character set.

Solution

There are two solutions:

  1. Run your html code through htmlentities before you write it to the form. That way your character reference stays a character reference.

  2. Use the correct character set "ISO-8859-7" for the greek language.

Example code

Use this code to see the problem/solution:

  1. <form method=post> 
    <?php
    echo $_POST['test'], '<br>';

    # Uncomment the following line to get it working ok.
    #$_POST['test'] = htmlentities($_POST['test']);

    echo <<<END
    <input type=text name=test value="$_POST[test]">
    END;
    ?>

  2. <html> 
    <head>
    <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-7">
    </head>
    <body>
    <form method=post enctype="multipart/form-data" accept-charset="ISO-8859-7">
    <?php
    print $_POST['test'];

    echo <<<END
    <input type=text name=test value="$_POST[test]">
    END;
    ?>

    </form></body></html>

BTW, you should use proper html not my poor excuse for it.

As you see this isnīt a problem of MySQL. It only appeared to be to you since you have a call to MySQL between repeated invocations of your script.

Hope this helps.

Andreas

andreasfriedrich

2:11 am on Nov 7, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This thread should probably be moved to the HTML forum.

Trisha

2:49 am on Nov 7, 2002 (gmt 0)

10+ Year Member



Thanks for the reply!

I don't fully understand your solution using htmlentities yet, but will check into it.

Trisha

7:17 am on Nov 7, 2002 (gmt 0)

10+ Year Member



I looked more closely at your example then I looked up htmlentities(), htmlspecialchars() and get_html_translation_table on php.net and phpbuilder - and realized it was all way, way over my head.

I figured I'd have to go back to my original solution of remembering if I used any non standard characters, and change them back to the correct decimal character reference before saving again.

Then in one last attempt to understand this - where I have:

echo $article_result[article];

I just wrapped htmlentities around it like this:

echo htmlentities($article_result[article]);

and OH MY GOD it worked! I didn't expect that, I thought it would be a lot more complicated!

Thanks!

andreasfriedrich

2:19 pm on Nov 7, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Trisha,

I had hoped my explanation would be precise enough to let you know what caused your error and to be able to fix it. Perhaps it wasnīt.

Anyway, Iīm glad you got it working at last.

Andreas