显示不同的语言问题


Displaying different language issue

我正在尝试显示不同的语言(土耳其语),但我无法显示它,而我可以在 HTML 中从我的数据库中看到它,但是当我回显土耳其字符 ç ğ ı ö ş ü 时我无法显示它像 § ❢ ₳ ₳ 在 PHP 中显示。虽然该文件应该保留在 PHP 中,因为我正在将其用于 iOS。那么请问我的问题在哪里?

<?php
$connection = mysql_connect($host, $user, $pass);
$queryA = '';
$decision = $_GET['x'];
$ilParam = $_GET['y'];

$ilSecim = 0;
$ilceSecim = 1;

//Check to see if we can connect to the server
if(!$connection)
{
    die("Database server connection failed.");  
}
else
{
//Attempt to select the database
        $dbconnect = mysql_select_db($db, $connection);
        //Check to see if we could select the database
        if(!$dbconnect)
        {
            die("Unable to connect to the specified database!");
        }
        else
        {          
      if($decision == $ilSecim)
        $queryA = "SELECT distinct city FROM places";
            $resultset = mysql_query($queryA, $connection);
            $records = array();
            //Loop through all our records and add them to our array
            while($r = mysql_fetch_assoc($resultset))
            {
                $records[] = $r;
            }
            //Output the data as JSON
            echo utf8_decode(json_encode($records));
        }
 }
?>

通常,在拉取数据库数据时,特殊字符是否保持良好取决于字符编码。

什么是字符编码

将字符编码想象成一个大表,其中每个字符都有他的数字表示形式 - 例如:

A = 1
B = 2
C = 3
..et cetera..

根据不同的文化和需求,有很多很多不同的编码表,一个是US-ASCII,另一个例如utf8或utf8_czech,或utf16,它们各自为不同的字符提供不同的数值(通常在不同的数字系统中,如十六进制等)。

解释

现在想象一下,您有文本,用一种编码编码,存储在数据库中的表中。您创建了一个数据库连接,该连接不知道表/列的字符编码,并且由于尚未告知要使用哪种字符编码,因此它使用默认的字符编码。

现在,您使用查询将数据从表中提取并读取它们,但由于数据库连接不知道正确的编码,因此它使用默认编码,该编码与您使用的编码接近,但不够接近。

因为当它从数据库中读取字符代码时,它会根据默认字符编码读取它们并赋予它们不同的含义 - 从 ç 变成 § 等......

溶液

所以基本上,你需要做的是指定数据库连接的字符编码。它是通过添加mysql_set_charset函数来完成的;

mysql_set_charset("charset_name_you_want_to_use",$database_connection_name);

在您的情况下:

mysql_set_charset("charset_name",$connection);

只需输入数据库中使用的字符集的名称而不是"charset_name",然后将其放在代码中的某个位置,最好是在选择数据库之后。

还应该知道,您正在使用PHP的一部分,这可能很快就会被弃用,如果您在版本高于5.0的PHP服务器上工作,则应使用mysql函数的其他替代方法,例如mysqli(它们都适用于mysql数据库,mysqli只是一种较新的首选方式)。

PS:我试图尽可能精确,但PHP不是我的事:),即使我有一些基本的经验。

在 http://www.happycode.info/php-json-response/中找到的一个很好的简单实现我会把它贴在这里,以防它消失

<?php
/**
* http://www.happycode.info
*/
function jsonResponse($param, $print = false, $header = true) {
    if (is_array($param)) {
        $out = array(
            'success' => true
        );
        if (is_array($param['data'])) {
            $out['data'] = $param['data'];
            unset($param['data']);
            $out = array_merge($out, $param);
        } else {
            $out['data'] = $param;
        }
    } else if (is_bool($param)) {
        $out = array(
            'success' => $param
        );
    } else {
        $out = array(
            'success' => false,
            'errors' => array(
                'reason' => $param
            )
        );
    }
    $out = json_encode($out);
    if ($print) {
        if ($header) header('Content-type: application/json');
        echo $out;
        return;
    }
    return $out;
}
?>

参数:

$param要输出的数组或字符串。

$print是否应打印或返回输出。默认行为是返回。

$header它是否应该发送内容类型标头。默认行为是发送。

示例 1

$response = array(
    array('id' => '1', 'name' => 'some name 1', 'created' => '2010-06-29 11:41:27', 'editable' => '1'),
    array('id' => '2', 'name' => 'some name 2', 'created' => '2010-07-07 16:47:03', 'editable' => '1'),
    array('id' => '3', 'name' => 'some name 3', 'created' => '2010-08-30 09:23:38', 'editable' => '1')
);
die(jsonResponse($response));

输出

{
    "success":true,
    "data":[
        {"id":"1","name":"some name 1","created":"2010-06-29 11:41:27","editable":"1"},
        {"id":"2","name":"some name 2","created":"2010-07-07 16:47:03","editable":"0"},
        {"id":"3","name":"some name 3","created":"2010-08-30 09:23:38","editable":"0"}
    ]
}

示例 2 返回错误

$error = 'Operation not permitted.';
die(jsonResponse($error));

输出

{
    "success":false,
    "errors":{
        "reason": "Operation not permitted."
    }
}

这样,您可以通过是否从第二个参数打印来测试它。