Bangalore
Bangalore

Reputation: 1580

JSON creating from PHP giving wrong data?

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

Upvotes: 1

Views: 190

Answers (2)

Rick James
Rick James

Reputation: 142298

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.

Upvotes: 1

ygesher
ygesher

Reputation: 1161

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

Upvotes: 0

Related Questions