简体   繁体   中英

UTF8 characters not displaying properly with datatables and yadcf

I have a mysql database that is in UTF8, I've properly output the letters before but cannot make my php do so with datatables using the ajax process. I have tried various methods to get the UTF8 to show, including, changing headers, using mysql functions, and changing the json encode function, but I cannot make it work. Please help me show UTF8 properly instead of ?s.

I have: temps.php My server side processing script:

 <?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) : "")
  ));

I have: temp.html: My Javascript function (based on datatables and 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
    },


]);
});

And I have temp.html where the data is outputted: (redacted because a lot of the code is irrelevant to the question)

 <!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>

Sorry for such a long question; but what else can I change to make UTF8 properly show on the html page? I've tried all the combinations of variables and functions I can think of; but can't make it work. Perhaps there is a js function that could be applied somewhere? Thank you for the help.

Edit: SQL structure for reference:

     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英');

You need to force utf8 in the PDO connection :

$db = SSP::db($sql_details);
$db->exec("set names utf8");

alternatively, try pass it as a param :

$sql_details = array(
  'user' => 'root', 
  'pass' => 'ryan', 
  'db'   => 'edata', 
  'host' => 'localhost', 
  'charset' => 'utf8' 
);

But this does not work with all PHP versions.

PS: Why do you set the table fields to be of type utf8 , but the table character set to be latin1 ?

If you have added all the columns as "UTF-8" and still the problem persists then try this

Add these lines on the server side file

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']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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