繁体   English   中英

如何显示从MySQL数据库到html的特殊字符?

[英]How to display special characters from MySQL database to html?

我有一个巨大的数据库,其中存储了许多语言。所有数据都存储在utf8_unicode_ci中,我的数据库排序规则是utf8_unicode_ci,并且我使用特殊的php页面来调用sql数据,这是我的代码:

<?php
header('Content-Type: text/html; charset=utf-8');
$date=$_POST['date'];//I get this information from ajax request
$server_root = "./";
if(file_exists("{$server_root}include-sql/mysql.class.php"))
{
    include_once("{$server_root}include-sql/mysql.class.php");
}
include_once("{$server_root}config.php");//database connection information
$db1;
$db1 = new db_mysql($conf['db_hostname'], $conf['db_username'], $conf['db_password'], $conf['db_name']);
$sql = $db1->query("select * from matches where match_date='$date'");
$i=0;
while($sql_results = $db1->fetch_array($sql))
{
    $match[$i]['tournament_id'] = $sql_results['tournament_id'];
    $match[$i]['id'] = $sql_results['id'];
    $match[$i]['match_id'] = $sql_results['match_id'];
    $match[$i]['match_date'] = $sql_results['match_date'];
    $match[$i]['match_time'] = $sql_results['match_time'];
    $match[$i]['match_status'] = $sql_results['match_status'];
    $match[$i]['match_venue'] = $sql_results['match_venue'];
    $match[$i]['venue_id'] = $sql_results['venue_id'];
    $match[$i]['static_id'] = $sql_results['static_id'];
    $match[$i]['match_week'] = $sql_results['match_week'];
    $match[$i]['localteam_name'] = $sql_results['localteam_name'];
    $match[$i]['localteam_score'] = $sql_results['localteam_score'];
    $match[$i]['localteam_ft_score'] = $sql_results['localteam_ft_score'];
    $match[$i]['localteam_et_score'] = $sql_results['localteam_et_score'];
    $match[$i]['localteam_pen_score'] = $sql_results['localteam_pen_score'];
    $match[$i]['localteam_id'] = $sql_results['localteam_id'];
    $match[$i]['visitorteam_name'] = $sql_results['visitorteam_name'];
    $match[$i]['visitorteam_score'] = $sql_results['visitorteam_score'];
    $match[$i]['visitorteam_ft_score'] = $sql_results['visitorteam_ft_score'];
    $match[$i]['visitorteam_et_score'] = $sql_results['visitorteam_et_score'];
    $match[$i]['visitorteam_pen_score'] = $sql_results['visitorteam_pen_score'];
    $match[$i]['visitorteam_id'] = $sql_results['visitorteam_id'];
    $match[$i]['halftime_score'] = $sql_results['halftime_score'];
    $match[$i]['stage_id'] = $sql_results['stage_id'];
    $i++;
}
echo json_encode($match);//return json to my ajax function
?> 

问题是mysql中的所有特殊字符都不会在浏览器中的html中显示或显示为null我要知道有什么我想念的吗? 如何在我的页面上显示这些字符

连接到MySQL后,尝试使用SET NAMES utf8

$db1 = new db_mysql($conf['db_hostname'], $conf['db_username'], 
    $conf['db_password'], $conf['db_name']);

$db1->query("SET NAMES utf8");

手册所述

SET NAMES指示客户端将用于将SQL语句发送到服务器的字符集...它还指定服务器用于将结果发送回客户端的字符集。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM