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