Unicode characters encoding for a MySQL output

Siddharthan Asokan

I'm trying to retrieve data from a table whose collation is 'utf8_general_ci' and the column titled 'app_name' has string like : Plants vs. Zombies™ 2,포코팡 for Kakao,네이버 - Naver,µTorrent® - Torrent App. When I tried to query the table as below:

   <?php

   $con = mysql_connect('localhost', 'root', 'root') or die('Error connecting to server');

   mysql_select_db('googleappanalysis', $con); 

  if(isset($_POST['appType']))
  {
    $appType = $_POST['appType'];
    $month = $_POST['month'];
    $year = $_POST['year'];
  }


  $monthYear = $month.$year; 

  mysql_query("SET NAMES utf8")

  if($appType == "Apps with IA" ){
    $sql="SELECT app_name  FROM MonthlyAppState WHERE is86 = 1 AND is86 IS NOT NULL AND monthYear = '".$monthYear."'";

    $result=mysql_query($sql);
   }elseif($appType == "Apps with no IA" ){
    $sql="SELECT app_name  FROM MonthlyAppState WHERE is86 = 0 AND is86 IS NOT NULL AND monthYear = '".$monthYear."'";
    $result=mysql_query($sql);
   }


    $table = array();
    $table['cols'] = array(
array('label' => $appType, 'type' => 'string')
 );


 $rows = array();
 $appendingString = "";
 while($r = mysql_fetch_assoc($result)){

    $temp = array();
$temp[] = array('v' => (string)$r['app_name']);
$rows[] = array('c' => $temp);
    $appendingString = $appendingString."/".$r['app_name'];
 }

 $myFile = "logfile.txt";  
 $fh = fopen($myFile, 'w') or die("can't open file"); 
 fwrite($fh, $appendingString);
 fclose($fh);

 $table['rows'] = $rows;

 // encode the table as JSON
 $jsonTable = json_encode($table);

  // set up header; first two prevent IE from caching queries
 header('Cache-Control: no-cache, must-revalidate');
 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
 header('Content-type: application/json');

 echo $jsonTable;
 ?>

The jsontable throws null values and for the file 'logfile.txt'the unicode characters is converted to '???' as seen here

zerkms

Use

mysql_set_charset('utf8', $con);

right after mysql_connect to set your client character set.

References:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Encoding for unicode and &amp; characters

From Dev

Unicode encoding for Polish characters in Python

From Dev

Python2.7, what does the special characters mean in the utf-32 encoding output of a unicode string?

From Dev

MySQL encoding weird characters

From Dev

Output encoding for special characters like !"#

From Dev

Unicode Characters issue in Cakephp and Mysql

From Dev

How to output HTML unicode characters from an expression

From Dev

Some Unicode characters not displayed in RMarkdown PDF output

From Dev

Swift: how to replace \U literal encoding into plain UNICODE characters?

From Dev

C/C++ Unicode characters encoding sizes and default formats

From Dev

How to Convert Unicode Characters to HTML Encoding C++?

From Dev

UTF-8 Unicode encoding and country specific characters

From Dev

C/C++ Unicode characters encoding sizes and default formats

From Dev

MySQL string contains only certain unicode characters

From Dev

mysql select not working with unicode characters a ä, d ḏ, ŋ

From Dev

Storing Unicode and special characters in MySQL tables

From Dev

How to output unicode characters and lines into an image file? -python

From Dev

Escape unicode characters in Go JSON so the output matches Python

From Dev

Unicode characters output from python I/O to files

From Dev

Make json.dumps output unicode characters properly in Python

From Dev

MySQL to JSON: Issue with encoding of German special characters in UTF-8

From Dev

UTF8 character encoding in Perl DBI, MySQL with ő and ű characters

From Dev

MySQL to JSON: Issue with encoding of German special characters in UTF-8

From Dev

UTF8 character encoding in Perl DBI, MySQL with ő and ű characters

From Dev

MOXy JAXB marshals invalid control characters for Unicode (u+2019) when UTF-8 encoding is specified

From Dev

How to change encoding from plain text to Unicode so that I can read special characters from a HTML?

From Dev

Replacing characters with unicode characters

From Dev

How to take unicode characters from the mysql data base in PHP

From Dev

Displaying random characters instead of Unicode in mysql database.?

Related Related

  1. 1

    Encoding for unicode and &amp; characters

  2. 2

    Unicode encoding for Polish characters in Python

  3. 3

    Python2.7, what does the special characters mean in the utf-32 encoding output of a unicode string?

  4. 4

    MySQL encoding weird characters

  5. 5

    Output encoding for special characters like !"#

  6. 6

    Unicode Characters issue in Cakephp and Mysql

  7. 7

    How to output HTML unicode characters from an expression

  8. 8

    Some Unicode characters not displayed in RMarkdown PDF output

  9. 9

    Swift: how to replace \U literal encoding into plain UNICODE characters?

  10. 10

    C/C++ Unicode characters encoding sizes and default formats

  11. 11

    How to Convert Unicode Characters to HTML Encoding C++?

  12. 12

    UTF-8 Unicode encoding and country specific characters

  13. 13

    C/C++ Unicode characters encoding sizes and default formats

  14. 14

    MySQL string contains only certain unicode characters

  15. 15

    mysql select not working with unicode characters a ä, d ḏ, ŋ

  16. 16

    Storing Unicode and special characters in MySQL tables

  17. 17

    How to output unicode characters and lines into an image file? -python

  18. 18

    Escape unicode characters in Go JSON so the output matches Python

  19. 19

    Unicode characters output from python I/O to files

  20. 20

    Make json.dumps output unicode characters properly in Python

  21. 21

    MySQL to JSON: Issue with encoding of German special characters in UTF-8

  22. 22

    UTF8 character encoding in Perl DBI, MySQL with ő and ű characters

  23. 23

    MySQL to JSON: Issue with encoding of German special characters in UTF-8

  24. 24

    UTF8 character encoding in Perl DBI, MySQL with ő and ű characters

  25. 25

    MOXy JAXB marshals invalid control characters for Unicode (u+2019) when UTF-8 encoding is specified

  26. 26

    How to change encoding from plain text to Unicode so that I can read special characters from a HTML?

  27. 27

    Replacing characters with unicode characters

  28. 28

    How to take unicode characters from the mysql data base in PHP

  29. 29

    Displaying random characters instead of Unicode in mysql database.?

HotTag

Archive