Reputation: 472
It's pretty straight forward. On one side I have a PHP file and on the other hand a html page with a javascript script to get the json string and do something with it.
I made sure all my files are in UTF-8, my database is too.
First test (it works): (PHP)
$array['fname'] = 'bob';
$array['lname'] = 'mozz';
$array['age'] = 20;
$array['email'] = '[email protected]';
echo json_encode($array);
Second test (failed) (PHP)
try{
$sql = 'SELECT * FROM blablabla';
$results = $pdo->query($sql);
$array = array();
while($row = $results->fetch()){
$array[] = array(
'id' => $row['id'],
'fname' => $row['fname'],
'lname' => $row['lname'],
'gender' => $row['gender'],
'organization' => $row['organization'],
'phoneLine1Label' => $row['phoneLine1Label'],
'phoneLine1' => $row['phoneLine1'],
'phoneLine1Ext' => $row['phoneLine1Ext'],
'phoneLine2Label' => $row['phoneLine2Label'],
'phoneLine2' => $row['phoneLine2'],
'phoneLine2Ext' => $row['phoneLine2Ext'],
'cellPhoneLabel' => $row['cellPhoneLabel'],
'cellPhone' => $row['cellPhone'],
'email' => $row['email']
);
}
$json = json_encode($array);
echo $json;
}catch(PDOException $e){}
I tried to convert my result to hexadecimal to see what is the unknown character:
In my javascript i have;
<script>
$(document).ready(function(){
var query = {
q: 'test',
apikey: '1234567890'
};
$.post('script.php',query,function(data,status){
switch(status){
case 'success':
obj = JSON && JSON.parse(data) || $.parseJSON(data);
console.log(obj);
break;
}
});
});
</script>
When i plug the script from my first PHP example, it works but not from my second one. It give me an error stating my json has unknown token.
I triple check everything and i just don't know what it might be.
Edit
Here is a capture from Fiddler web debugger, it's just more strange:
Edit 2 I even used trim like :
$json = json_encode($rows);
$fix = substr($json,7);
$fix = '[{"id":'.$fix;
But it still add and unknown character add the beginning when i receive it.
Edit 3 (solution) I empty the file, save it as utf8 without BOM and paste back the content and it did the job. I still don't understand why it did that. I do understand BOM create issue but the file containing my PHP was a controller containing other instruction and those instruction which were heavily based-upon encoding, were working perfectly. Big thanks to anyone for taking their time looking into my problem, I do really appreciate it!
Upvotes: 0
Views: 1399
Reputation: 472
I empty the file, save it as utf8 without BOM and paste back the content and it did the job. I still don't understand why it did that. I do understand BOM create issue but the file containing my PHP was a controller containing other instruction and those instruction which were heavily based-upon encoding, were working perfectly. Big thanks to anyone for taking their time looking into my problem, I do really appreciate it!
Upvotes: 0
Reputation: 197842
This smells like you've got a BOM in the front. Save PHP files without BOM and that should do it. :)
See as well: UTF-8 BOM signature in PHP files
Upvotes: 2