<?php
   $route = intval($_GET['route']);

   $link = mssql_connect('localhost\SQLEXPRESS','***','*********') or die("Couldn't connect to SQL Server on $myServer. Error: " . mssql_get_last_message());
   mssql_select_db('PDQ',$link) or die('Cannot select the DB');

   $query = "SELECT * FROM CUSTOMER WHERE CUS_ROUTE = $route";
   $result = mssql_query($query,$link) or die('Errant query:  '.$query);

   $rows = array();
   while($row = mssql_fetch_assoc($result)) {
      $rows[] = $row;
   }
   echo json_encode($rows);


   @mssql_close($link);
?>

My Result is:

[
{
CUS_DLT: 2,
CUS_COMPANY: 1,
CUS_CUSTOMSTRING2: "TIMES MARKET # 9"
},
{
CUS_DLT: 2,
CUS_COMPANY: 1
CUS_CUSTOMSTRING2: "TIMES MARKET # 8"
}
]

However this is not a valid json_encode because the column names are not wrapped in ". Does anyone know why or how to fix this?

    PHP Version 5.2.14. It must be the php version because when i test with

    <?php
    $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
    
    echo json_encode($arr);
    ?>

    I get result

    {
    a: 1,
    b: 2,
    c: 3,
    d: 4,
    e: 5
    }

      Also unable to reproduce the problem in 5.3.9 and 5.3.10.

      <?php
      
      $array = array(
         array('CUS_DLT' => 2, 'CUS_COMPANY' => 1, 'CUS_CUSTOMSTRING2' => 'TIMES MARKET # 9'), 
         array('CUS_DLT' => 2, 'CUS_COMPANY' => 1, 'CUS_CUSTOMSTRING2' => 'TIMES MARKET # 8') 
      ); 
      
      echo json_encode($array, JSON_PRETTY_PRINT); 
      [{"CUS_DLT":2,"CUS_COMPANY":1,"CUS_CUSTOMSTRING2":"TIMES MARKET # 9"},{"CUS_DLT":2,"CUS_COMPANY":1,"CUS_CUSTOMSTRING2":"TIMES MARKET # 8"}]

        Thanks guys. The hosting company i'm using is on this version of php i suppose. I'll have to see if i can get them to upgrade because that's ridiculous. arvixe.com is who i am using.

          Dunno about them specifically but I know my host keeps people on their current version unless they specifically ask to be moved to a newer version. They do it "to make sure customers websites continue to function until they can properly update their scripts to the new version." so I would imagine you can probably send support an email and get at least onto 5.3.x

            They came highly recommended from a few places so hopefully that is the case. Thank you all for your time. Should have thought about the version earlier.

              After further investigation i have determined that it is not php that is causing the json to be formatted incorectly but google chrome that automatically takes out the double quotes when displaying the json.

                Good ol' Google. Knowing what you "want" before you want it.

                  Interesting - my browser of choice is google chrome, and it didn't remove them for me when I reported that it worked O_o

                    must be a setting :/ or build version of google chrome. so many variablessssssssss haha

                      Write a Reply...