I created a PHP code to fetch information from a database (MySQL) and "transform" into JSON. I used json_encode
to do this.
The output JSON seemed to be correct, but when I use some JSON validator, it always returns a Unexpected token
error.
What puzzles me most is that if I manually enter the output JSON in the validator, it works! If I give Ctrl + C and Ctrl + V, the error occurs.
To check the JSON result: link
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;
?>