【问题标题】:MySql UTF8 collation data with accents not displayed properly when PHP fetched获取 PHP 时,带有重音符号的 MySql UTF8 排序数据无法正确显示
【发布时间】:2012-04-15 15:24:02
【问题描述】:

我正在使用 PHP 从 MySQL 数据库中获取一些具有 UTF8 排序规则 (utf8_unicode_ci) 的数据。我正在使用这段代码:

function advancedDatabaseSearch($pattern, $lpref) {

    $link = mysql_connect(DB_URL, DB_USER, DB_PWD);

    if (!$link) {
        return 'Could not connect: ' . mysql_error();
    }

    $esc_value = mysql_real_escape_string($pattern);
    $esc_lpref = mysql_real_escape_string($lpref);

    mysql_select_db(DB_NAME, $link);

    $query = "SELECT RAWVALUE FROM rawvalueitem "
        ."WHERE RAWVALUE LIKE '".$esc_value."' "
        ."AND LANGUAGE = '".$esc_lpref."' "
        ."ORDER BY RAWVALUE ASC";

    $result = mysql_query($query);

    $return = "";

    while($row = mysql_fetch_array($result)) {
        $return = $return.$row['RAWVALUE']." ";
    }

    mysql_close($link);

    return $return;

}

然后从Ajax调用的php中:

$result = advancedDatabaseSearch($tttmp, $lpref);
echo $result;
return;

然而,当我在文本区域中显示结果时,重音显示不正确:

另一方面,当我从文件中获取 UT8 数据时:

if ( $file_loc != NULL ) {
    if ( file_exists($file_loc) ) {
        $handle = fopen($file_loc, "rb");
        $contents = fread($handle, filesize($file_loc));
        fclose($handle);
        $result = $contents;
    }
}

echo $result;
return;

我不明白这个问题!使用PHP从MySql取数据时如何解决?

【问题讨论】:

    标签: php mysql ajax non-ascii-characters


    【解决方案1】:

    这就像一个..你现在!

    <?php
        $config_db_server='localhost';
        $config_db_server_username='root';
        $config_db_server_password='';
        $config_db_database='test';
        $config_db_charset='utf8';
        $config_db_collation='utf8_general_ci';
        $config_table_prefix='class_';
        $config_live_site='http://localhost';
        $config_abs_path='C:\xampp\htdocs';
        $config_debug=0;
    
        $dbLink = mysql_connect($config_db_server, $config_db_server_username, $config_db_server_password);
        mysql_query("SET character_set_results=utf8", $dbLink);
        mb_internal_encoding('utf8');
        mysql_query("set names 'utf8'",$dbLink);
    

    ?>

    在此处更改您的数据库连接:( $dbLink = mysql_connect($config_db_server, $config_db_server_username, $config_db_server_password); )

    【讨论】:

      【解决方案2】:

      您是否将 UTF-8 设置为数据库连接的默认字符集?

      mysql_set_charset('utf8', $link);

      http://www.php.net/manual/en/function.mysql-set-charset.php

      另外,您的网页是否有带有正确字符集的&lt;meta&gt; 标记?

      【讨论】:

      • 不,我没有。我不知道这件事。谢谢,它解决了这个问题。
      猜你喜欢
      • 2021-12-24
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      • 2019-05-16
      • 2014-08-03
      相关资源
      最近更新 更多