1

I have one php form where i used to enter data to database(phpmyadmin), and i used SELECT query to display all values in database to view in php form.

Also i have another PHP file which i used to create JSON from the same db table.

Here when i enter foreign languages like "Experiența personală:" the value getting saved in DB is "ExperienÈ›a personală: " but when i use select query to display this in same php form it coming correctly "Experiența personală:". So the db is correct and now am using following php code to create JSON

    <?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "aaps";

// Create connection
$con=mysqli_connect($servername,$username,$password,$dbname);
// Check connection
mysqli_set_charset($con, 'utf8');
//echo "connected";
$rslt=mysqli_query($con,"SELECT * FROM offers");
while($row=mysqli_fetch_assoc($rslt))
{

$taxi[] = array('code'=> $row["code"], 'name'=> $row["name"],'contact'=> $row["contact"], 'url'=> $row["url"], 'details'=> $row["details"]);
}
header("Content-type: application/json; charset=utf-8");

echo json_encode($taxi);
?>

and JSON looks like

[{"code":"CT1","name":"Experien\u00c8\u203aa personal\u00c4\u0192: ","contact":"4535623643","url":"images\/offers\/event-logo-8.jpg","details":"Experien\u00c8\u203aa personal\u00c4\u0192:  jerhbehwgrh  234234 hjfhjerg#$%$#%#4"},{"code":"ewrw","name":"Experien\u00c8\u203aa personal\u00c4\u0192: ","contact":"ewfew","url":"","details":"eExperien\u00c8\u203aa personal\u00c4\u0192: Experien\u00c8\u203aa personal\u00c4\u0192: Experien\u00c8\u203aa personal\u00c4\u0192: "},{"code":"Experien\u00c8\u203aa personal\u00c4\u0192: ","name":"Experien\u00c8\u203aa personal\u00c4\u0192: ","contact":"","url":"","details":"Experien\u00c8\u203aa personal\u00c4\u0192: "}]

In this "\u00c8\u203aa" this is wrong it supposed to be "\u021b" (t).

So pho used to creating JSON making this issue.

But am unable to find exactly why its coming like this . please help

Bangalore
  • 1,572
  • 4
  • 20
  • 50
  • Are you sure the data from the database is correct? Try doing a `var_dump` of the `$taxi` array before encoding it. – Jerodev Sep 24 '15 at 14:50
  • yes db is correct becoz when i tried to do select query on same php file am getting correct date.ok i will do and let u know – Bangalore Sep 24 '15 at 14:52
  • array(4) { [0]=> array(5) { ["code"]=> string(3) "CT1" ["name"]=> string(29) "ExperienÈ›a personală: " ["contact"]=> string(10) "4535623643" ["url"]=> string(30) "images/offers/event-logo-8.jpg" ["details"]=> string(66) "ExperienÈ›a personală: jerhbehwgrh 234234 hjfhjerg#$%$#%#4" } [1]=> array(5) { ["code"]=> string(4) "ewrw" – Bangalore Sep 24 '15 at 15:00
  • var dump coming like that – Bangalore Sep 24 '15 at 15:01

2 Answers2

1

Avoid Unicode -- note the extra argument:

json_encode($s, JSON_UNESCAPED_UNICODE)

Don't use utf8_encode/decode.

ă turning into ă is Mojibake. It probably means that

  • The bytes you have in the client are correctly encoded in utf8 (good).
  • You connected with SET NAMES latin1 (or set_charset('latin1') or ...), probably by default. (It should have been utf8.)
  • The column in the tables may or may not have been CHARACTER SET utf8, but it should have been that.

If you need to fix for the data it takes a "2-step ALTER", something like

ALTER TABLE Tbl MODIFY COLUMN col VARBINARY(...) ...;
ALTER TABLE Tbl MODIFY COLUMN col VARCHAR(...) ... CHARACTER SET utf8 ...;

Before making any changes, do

SELECT col, HEX(col) FROM tbl WHERE ...

With that, ă should show hex of C483. If you see C384C692, you have "double-encoding", which is messier to fix.

Rick James
  • 135,179
  • 13
  • 127
  • 222
0

Depending on the version of MySql in the database, it may not be using the full utf-8 set, as stated in the documentation:

The ucs2 and utf8 character sets do not support supplementary characters that lie outside the BMP. Characters outside the BMP compare as REPLACEMENT CHARACTER and convert to '?' when converted to a Unicode character set.

This, however, is not likely to be related to your problem. I would try a couple of different things and see if it solves your problem.

  1. use SET NAMES utf-8 You can read more about that here
  2. use utf8_encode() when inserting data to the database, and utf8_decode() when extracting. That way, you don't have to worry about MySql manipulating the unicode characters. Documentation
Community
  • 1
  • 1
ygesher
  • 1,133
  • 12
  • 26