Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php json_encode not working on arrays partially

Tags:

json

php

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..

like image 569
vsriram92 Avatar asked Nov 15 '13 11:11

vsriram92


1 Answers

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.

like image 173
Wayne Whitty Avatar answered Sep 24 '22 01:09

Wayne Whitty