Why is this PHP call to json_encode silently failing - inability to handle single quotes?
You need to set the connection encoding before executing queries. How this is done depends on the API you are using to connect:
- call
mysql_set_charset("utf8")
if you use the old, deprecated API. - call
mysqli_set_charset("utf8")
if you use mysqli - add the
charset
parameter to the connection string if you use PDO and PHP >= 5.3.6. In earlier versions you need to executeSET NAMES utf8
.
When you obtain data from MySQL any text will be encoded in "client encoding", which is likely windows-1252 if you don't configure it otherwise. The character that is causing your problem is the "curly quote", seen as 92
in the hex dump, which confirms that the mysql client is encoding text in windows-1252.
Another thing you might consider is pass all text through utf8_encode
, but in this case it wouldn't produce the correct result. PHP's utf8_encode
converts iso-8859-1-encoded text. In this encoding \x92 is a non-printable control character, which would be converted into a non-printable control character in utf-8. You could use str_replace("\x92", "'", $input)
to fix the problem for this particular character, but if there's any chance there will be any other non-ascii characters in the database you'll want to have the client use UTF-8.
PHP: json_encode() doesn't show anything with multidimensional array
User @Joni led me to the solution.
Adding mysql_set_charset("utf8")
fixed my issue.
As mentioned in this post: Why is this PHP call to json_encode silently failing - inability to handle single quotes?.
Why would json_encode return false in this case?
From PHP's manpage for json_last_error():
JSON_ERROR_UTF8 Malformed UTF-8 characters, possibly incorrectly encoded (PHP 5.3.3)
You have to ensure that the data given to json_encode() is UTF8, you should try using iconv to convert your strings to a proper format.
Php array from query to json is not working
My recollection is that php's json encode is sensitive to character sets, and in particular enforces the (standard for JSON) that chars are in UTF-8.
You may have to re-encode your text into UTF-8 before you encode it.
It is possible that doing this on the serialize() string, then unserializing, then json encode, is the fastest way.
PHP: json_encode() doesn't show anything with multidimensional array
User @Joni led me to the solution.
Adding mysql_set_charset("utf8")
fixed my issue.
As mentioned in this post: Why is this PHP call to json_encode silently failing - inability to handle single quotes?.
json_encode returns false when dealing with multibyte substring
Simply pass the utf-8
encoding as the fourth parameter of the mb_substr()
and you are good to go.
$a = mb_substr($s, 0, 10,'utf-8');
echo $a; // に搭載されるようにな
echo json_encode($a); // "\u306b\u642d\u8f09\u3055\u308c\u308b\u3088\u3046\u306b\u306a"
Demonstration
json_decode fails on godaddy hosting
For whatever reason, on godaddy hosting only (maybe in some other places too, but not in other places I tested), when I was submitting the form, the server was escaping quotes - and that's what was causing the issue. The solution to the problem was quite simple: instead of doing
$data = json_decode($_POST['logdata'], true);
I did
$data = json_decode(stripslashes($_POST['logdata']), true);
and everything worked perfectly.
Related Topics
Why Is Pdo Better For Escaping MySQL Queries/Querystrings Than MySQL_Real_Escape_String
Warning: Domdocument::Loadhtml(): Htmlparseentityref: Expecting ';' in Entity,
Session Data Lost in Chrome Only
How to Get Everything After a Certain Character
How to Get Response Using Curl in PHP
Fatal Error: Call to Undefined Function MySQLi_Result()
Is There an Equivalent For Var_Dump (PHP) in JavaScript
Connect to a MySQL Server Over Ssh in PHP
PHP Convert Date Format Dd/Mm/Yyyy =≫ Yyyy-Mm-Dd
Can't Access Global Variable Inside Function
Notice: Array to String Conversion In
How to Convert Many Statement MySQL to Laravel Eloquent
How to Embed Youtube Videos in PHP
Change Div Content Using Ajax, PHP and Jquery
Fatal Error: Call to Undefined Function Mb_Detect_Encoding()