Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Chinese characters in PHP

    Client page:

    <head>
    <meta http-equiv="Catchup Scheduler" content="text/html; charset=UTF-8">
    </head>

    <body>
    <form action="next.php" method="POST" >
    <input type="text" name="programme_name"></br>
    <input type="submit">
    </form>
    </body>
    Server page:

    $name = $_POST['programme_name'];

    mysql_query("SET character_set_client=utf8",$con);
    mysql_query("SET character_set_connection=utf8", $con);
    mysql_query("SET character_set_results=utf8", $con);

    $query = "Select * from programme where Programme_name Like '%".$name."'";
    Problem is even if there is that chinese text in the database results is ZERO(none). If I print the POST value $name it displays the correct text with the chinese characters.

    but if i store it in a variable, such as:

    $name= "valuevalue ( 官话 )";
    $query = "Select * from programme where Programme_name Like '%".$name."'";
    It produces results. how could this be, in POST it doesn't but static value declaration it does? My OS is XP SP3.

    Is there a problem in the "internal" representation of chinese characters if it is in POST? Do I need to set something in my OS to support chinese representation?

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,479
    Thanks
    63
    Thanked 538 Times in 525 Posts
    The problem is your encoding - both in the page and the mysql collation. You need to use utf8 for both. This opens up a minefield of problems as I recently found out when having to switch to utf8 for a project.

    Using notepad++ you need to encode your html as utf8 without bom yet still encode the pho files as ansii whilst everything going in and out of the database needs to be utf8. Confused? You're not the only one. To make matters worse I found that pages on my system would output different characters to the pages on the live webserver which then made it pretty much impossible for me to develop the code locally before uploading.

    Fou-Lu is far better with this stuff than me and recently got me out of a very big hole regarding this stuff so you may be best waiting for further advice from them.
    I can't really think of anything to write here now...

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yeah php doesn't natively support unicode, and it won't until version 6.

    I'd visually compare each of these queries. Write one query with the characters explicitly in it (the one that works), and call it $query1. Then write another one that takes the $_POST and call it $query2. Evaluate each of the characters within them. I'll make a table (with poor standards :P):
    PHP Code:
    $query1 'cat'// put your queries here.
    $query2 'mouse';

    $iMaxLength max(strlen($query1), strlen($query2));

    print 
    '<table border="1">';
    for (
    $i 0$i $iMaxLength; ++$i)
    {
        
    $cl = isset($query1[$i]) ? $query1[$i] : null;
        
    $cr = isset($query2[$i]) ? $query2[$i] : null;
        
    printf('<tr><td>%1$s</td><td>%3$s</td><td>%2$d</td><td>%4$d</td></tr>'$clord($cl), $crord($cr));
    }
    print 
    '</table>'
    Do the last two columns match row by row? Another good thing to try is to use mb_strlen instead of strlen in the max test to see if it provides different results.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •