Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

json - php json_encode not working on arrays partially

I have a PHP code which needs to encode DB table datas into json. So I used json_encode().

I use the tables given here - http://www.geekality.net/2011/08/21/country-names-continent-names-and-iso-3166-codes-for-mysql/

The behavious of this code seems to be different for different inputs.

The query - $query = "SELECT * FROM countries "; doesn't return any json values.

The query -$query = "SELECT * FROM countries where continent_code='AS'"; returns json values correctly.

whereas,$query = "SELECT * FROM countries where continent_code='EU'"; also does't return anything.

Similarily 'NA','AF' did not work and others work perfect.

I'm weird of this behaviour of PHP's json_encode.

This is my code.

<?php
$con=mysqli_connect('localhost','xxxx','xxxxx','joomla30');
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM countries where continent_code='EU'") or die (mysqli_error($con));

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
      $orders[] = array(
          'CountryCode' => $row['code'],
          'CountryName' => $row['name']
          );
  }
//print_r($orders);

echo json_encode($orders);

mysqli_close($con);
?>

Until the previous line of json_encode, the datas are carried. So I think its json encode problem.

I tried to know about it using print_r($orders);.
I got the following output from it.

If I try for 'EU', i get this array.

Array ( [0] => Array ( [CountryCode] => AD [CountryName] => Andorra ) 
[1] => Array ( [CountryCode] => AL [CountryName] => Albania ) 
[2] => Array ( [CountryCode] => AT [CountryName] => Austria ) 
[3] => Array ( [CountryCode] => AX [CountryName] => ?land Islands )...

If I try for 'AS', i get this array.

Array ( [0] => Array ( [CountryCode] => AE [CountryName] => United Arab Emirates ) 
[1] => Array ( [CountryCode] => AF [CountryName] => Afghanistan) 
[2] => Array ( [CountryCode] => AM [CountryName] => Armenia) 
[3] => Array ( [CountryCode] => AZ [CountryName] => Azerbaijan)...

Both the arrays looks alike right... But Json_encode works only on 'AS' and not for 'EU'.

Does anyone know how to solve this problem ? If so, pls tell me..

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should make sure that every component of your web application uses UTF-8. This answer from another question will tell you how to do this. If you read this from RFC4627:

Encoding: JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.

The json_encode function requires all incoming data to be UTF-8 encoded. That's why strings such as ?land Islands are probably causing you problems.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...