javascript - UTF8 字符无法正确显示数据表和 yadcf

标签 javascript php mysql utf-8 datatables

我有一个 UTF8 格式的 mysql 数据库,我之前已经正确输出了这些字母,但无法让我的 php 使用 ajax 进程对数据表执行此操作。我尝试了各种方法来让 UTF8 显示,包括更改 header 、使用 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

关于javascript - UTF8 字符无法正确显示数据表和 yadcf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31064740/

相关文章:

mysql - 为 MySQL 导出到 csv 添加额外的列?

javascript - 滚动父框架以锚定在 iframe 中

python - 从 sqlite 表中提取项目以在 Python 中使用时遇到问题

javascript - 如何使用 React-router-dom 版本 6 useNavigate 和 typescript 传递参数

php - 循环嵌套菜单

php - 使用 PHP 从 mySQL 获取记录并将其发送到 VB.net

javascript - 使用Jquery刷新表

MySQL 以日期时间间隔连接两个表

javascript - 如何在 sequelize.js 中有条件地加载关联?

javascript - 如何配置 Webpack 输出以仅包含特定包的一些选项