vsriram92
vsriram92

Reputation: 663

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

Upvotes: 13

Views: 34264

Answers (2)

user399666
user399666

Reputation: 19879

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.

Upvotes: 28

deceze
deceze

Reputation: 522083

Just a random guess here: json_encode requires all data you feed in to be UTF-8 encoded and it will fail otherwise. These countries where it fails, you likely have data which contains non-ASCII characters. Pure ASCII happens to be compatible with UTF-8, non-ASCII characters you need to take special care of. See UTF-8 all the way through for how to get UTF-8 encoded data out of your database (use mysqli_set_charset).

Upvotes: 15

Related Questions