0

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'] = 'test@test.com';
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:

  • when i print directly from the script i see nothing wrong (no special character)
  • when i print the result in my console, it seem great but
  • when i copy the result in my clipboard and paste it in a utf8 file, it's great
  • when i copy the result in an ansi file, i see a "?" at the beginning of it enter image description here
  • when i do a right click from the console (in google chrome) google see a square at the beginning of the string (invisible at the eye)

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:

enter image description here

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!

happy
  • 474
  • 4
  • 17
  • have you tried using `trim` on your array before you `json_encode` it ? eg `json_encode(array_map('trim',$array));` – Manse Jun 06 '13 at 14:48
  • Can you copy/past the server response ? – Brewal Jun 06 '13 at 14:51
  • @ManseUK i tried yea, no success – happy Jun 06 '13 at 14:52
  • Are you sure you don't have a space or something before your php opening tag ? – Brewal Jun 06 '13 at 14:53
  • @Brewal yes, rewrote it several time from scratch – happy Jun 06 '13 at 15:00
  • Can you get the character in database that causes the issue ? – Brewal Jun 06 '13 at 15:03
  • @Brewal i updated my question to include a fiddler snapshot. It's strange because the special character is not from the database (utf8) but from within the key value of the row.. that is so strange – happy Jun 06 '13 at 15:11
  • Try to put this header : `header('Content-type: application/json');` – Brewal Jun 06 '13 at 15:22
  • it might help to know what exactly the json thats failing looks like. IE. are you sure that the code making that array from the database is even working? – Rooster Jun 06 '13 at 16:22
  • It may or may not help but I use this tool alot as I work with JSON everyday. It gives line number and character number when erroring. It might be worth a shot: http://tomeko.net/software/JSONedit/ (to download scroll to bottom of the page) – azzy81 Jun 06 '13 at 16:26

2 Answers2

2

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

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
0

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!

happy
  • 474
  • 4
  • 17
  • See the link in my answer. PHP will always output it to the browser, the controller file only needs to be loaded (include/require). – hakre Jun 06 '13 at 16:32