13

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

1
  • 1
    I guess, the Problem is the "Åland Islands" because json_encode has problems with the "Å" for being a weird unicode. Try setting your mysql_connection to UTF-8 Commented Nov 15, 2013 at 11:12

2 Answers 2

28

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your quick response within 2 minutes.. I added mysqli_set_charset($con, 'utf8'); It worked perfect...
15

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

1 Comment

Thanks, i just added the following code $db->set_charset("utf8"); in my prepared statement and work perfectly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.