How to manage ID in database with Japanese charact

2019-09-18 07:05发布

问题:

Now I got a database. The database is encoded with the almighty utf-8 collation. Actually collation is utf8, I am not sure what the encoding is. That should be another question.

Then I made a program to retrieve data from the database.

<?php
    require_once('convertArraytoJson.php');
    require_once('config.php');
    mysql_connect ( "localhost", $databaseuser, $databasepassword);
    mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
    @mysql_select_db ($databasename) or die ( "Unable to select database" );
    $data=$_GET['id'];
    $query="SELECT * FROM `tabletes` where id = '".$data."'";
    $data = mysql_query($query);
    while (true){
        $info = mysql_fetch_array ( $data, MYSQL_ASSOC );
        if ($info == false) {
            break;
        }
        //$output[]=$info;
        $output[$info['ID']]=$info;
        unset ($output[$info['ID']]['ID']);
    }
    $result = array2json($output);

    echo $result;
?>

The content of the database looks like this:

Now I call the function by doing this (you need to enlarge your screen to see it):

http://localhost/domainname/api/test2.php?id=jr-東北本線-荒川橋梁__35.79_139.72

It doesn't work.

However, if I do NOT use $_GET but simply enter the Japanese characters directly in the code it works.

So if I change:

$data = $_GET['id']

to

$data = 'jr-東北本線-荒川橋梁__35.79_139.72'

Things are working fine.

Of course, I don't want to hardwire the ID, I want to access that via $_GET['id']. What should I do?

回答1:

Just use urlencode() - your string becomes like:

%E6%9D%B1%E5%8C%97%E6%9C%AC%E7%B7%9A-%E8%8D%92%E5%B7%9D%E6%A9%8B%E6%A2%81.

These Chinese characters are not allowed.

Check rfc1738: Uniform Resource Locators (URL)



回答2:

You can not use Japanese characters directly into the url using utf-8. You need to use url encoding to pass the parameter.

Look at the following link too.

Japanese characters in URL/FTP server