我有一个使用UTF8的mysql数据库,我之前已经正确地输出了字母,但无法使用ajax进程使我的php对数据表进行输出。我尝试了各种方法来显示UTF8,包括更改标题、使用mysql函数和更改json编码函数,但我无法使其工作。请帮我正确显示UTF8,而不是?s.
我有:temps.php我的服务器端处理脚本:
<?php
header("Content-Type: text/json; charset=utf-8");
require( 'ssp.class.php' );
$table = 'articles';
$primaryKey = 'id';
$columns = array(
array( 'db' => 'title', 'dt' => 0 ),
array( 'db' => 'description', 'dt' => 1 ));
$sql_details = array(
'user' => 'root', 'pass' => 'pass', 'db' => 'test', 'host' => 'localhost'
);
$db = SSP::db($sql_details);
.....
if($clause !== ""){
$whereAll[] = $clause;
$_GET['columns'][$i]['search']['value'] = "";
}}
echo json_encode(
SSP::complex( $_GET, $db, $table, $primaryKey, $columns, null,
(!empty($whereAll) ? implode(" AND ", $whereAll) : "")
));
我有:temp.html:我的Javascript函数(基于数据表和yadcf)
$(document).ready(function(){
$("#example").dataTable({
"responsive": true,
"processing": true,
"serverSide": true,
"ajax": "scripts/temps.php",
"columns": [
// ID
null,
// Distributor
null
// Director
]
}).yadcf([
// ID
{
column_number: 0 ,
filter_type: "text",
filter_reset_button_text: false
},
// Abstract
{
column_number: 1,
filter_type: "text",
filter_delay: 500,
filter_reset_button_text: false
},
]);
});
我有temp.html,其中输出数据:(由于许多代码与问题无关,所以进行了编辑)
<!DOCTYPE html>
<div>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="Description" CONTENT=""/>
..........
<table id="example" class="table table-striped table-bordered table-condensed" cellspacing="0" width="100%">
<thead>
<tr>
<th><div>One</div></th>
<th><div>Two</div></th>
</tr>
</thead>
</table>
抱歉问了这么长的问题;但是,我还可以更改什么以使UTF8正确地显示在html页面上?我已经尝试了我能想到的所有变量和函数的组合;但无法使其工作。也许有一个js函数可以应用于某个地方?谢谢你的帮助。
编辑:SQL结构供参考:
CREATE TABLE IF NOT EXISTS `articles` (
`id` int(11) NOT NULL,
`title` varchar(300) CHARACTER SET utf8 DEFAULT NULL,
`description` text CHARACTER SET utf8
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
-- Dumping data for table `articles`
INSERT INTO `articles` (`id`, `title`, `description`) VALUES
(1, 'శ్రీనివాస్scddadas తామాడా', '新概念英语第asv'),
(2, 'asda', 'asdవా'),
3, 'sdfsadfsdf英语', 'sadf英');
您需要在PDO连接中强制utf8
:
$db = SSP::db($sql_details);
$db->exec("set names utf8");
或者,尝试将其作为参数传递:
$sql_details = array(
'user' => 'root',
'pass' => 'ryan',
'db' => 'edata',
'host' => 'localhost',
'charset' => 'utf8'
);
但这并不适用于所有的PHP版本。
PS:为什么将表字段设置为类型utf8
,而将表字符设置为latin1
?
如果您已将所有列添加为"UTF-8",但问题仍然存在,请尝试此
在服务器端文件上添加这些行
mysql_query("SET character_set_client=utf8",$gaSql['link']);
mysql_query("SET character_set_connection=utf8",$gaSql['link']);
mysql_query("SET character_set_results=utf8",$gaSql['link']);