4
I created a PHP code to fetch information from a database (Mysql) and "transform" into JSON. I used the json_encode
for such.
The output JSON seemed to be correct, but when I use some JSON validator, it always returns an error Unexpected token
.
What most intrigues me is that if I manually type the output JSON into the validator, it works! If I give a Ctrl+C and Ctrl+V, the error occurs.
To check the JSON result: http://devsa.url.ph/? Cod=all
What can it be?
<?php
include('connectdb.php');
$something = $_GET['cod'];
$sqlcode = mysql_query("Select descricao from Produtos Where codigo='$something'");
$sqlcode2 = mysql_query("Select descricao from Produtos");
$jsonObj = array();
if ($something == 'all') {
while ($result = mysql_fetch_object($sqlcode2)) {
$jsonObj[] = $result;
}
} else {
while ($result = mysql_fetch_object($sqlcode)) {
$jsonObj[] = $result;
}
}
$final_res = json_encode($jsonObj);
echo $final_res;
?>
Note that there is some character before opening the bracket. Use the directional arrows that you will notice.
– Paulo Rodrigues