3

php to connect and get all rows

$query = 'SELECT * FROM cdr'; 
$sql = $remotedbh->prepare($query);  
$sql->execute(); 
$dblist = $sql->fetchAll(PDO::FETCH_ASSOC); 

if($sql->rowCount() > 0){ 
    header('Content-type: application/json');
    echo json_encode($dblist,JSON_FORCE_OBJECT); 
} 
else {
    echo 0; 
}  

The problem is my json looks like this

{
"0": {
    "calldate": "2013-08-14 11:41:28",
    "clid": "\"tet\" <1002>",
    "src": "1002",
    "dst": "8834404",
    "dcontext": "from-internal",
    "channel": "SIP\/1002-00000000",
    "dstchannel": "IAX2\/voipms-6749",
    "lastapp": "Dial",
    "lastdata": "IAX2\/voipms\/14798834404,300,",
    "duration": "7",
    "billsec": "0",
    "disposition": "NO ANSWER",
    "amaflags": "3",
    "accountcode": "",
    "uniqueid": "1376498488.1",
    "userfield": "",
    "did": "",
    "recordingfile": "",
    "cnum": "",
    "cnam": "",
    "outbound_cnum": "",
    "outbound_cnam": "",
    "dst_cnam": ""
},
"1": {
    "calldate": "2013-08-14 11:42:55",
    "clid": "\"Rtest\" <1002>",
    "src": "1002",
    "dst": "9187755592",
    "dcontext": "from-internal",
    "channel": "SIP\/1002-00000001",
    "dstchannel": "IAX2\/voipms-121",
    "lastapp": "Dial",
    "lastdata": "IAX2\/voipms\/19187755592,300,",
    "duration": "494",
    "billsec": "485",
    "disposition": "ANSWERED",
    "amaflags": "3",
    "accountcode": "",
    "uniqueid": "1376498575.3",
    "userfield": "",
    "did": "",
    "recordingfile": "",
    "cnum": "",
    "cnam": "",
    "outbound_cnum": "",
    "outbound_cnam": "",
    "dst_cnam": ""
},
"2": {
       so on so forth

I also am having the problem of everytime it pulls all the information a row is only half complete so it has a problem with that aswell, I was thinking of finding away to query the everything that is 5 minutes or older.

4

2 回答 2

2

It's including the row numbers as you're using JSON_FORCE_OBJECT

Remove it and it it'll successfully return as an array.

e.g.

echo json_encode($dblist); 
于 2013-09-05T14:30:13.260 回答
1

JSON_FORCE_OBJECT option in json_encode() forces the array to a json object with its indexes as keys. Removing that might get working for you.

update :

because while testing with a sample array

$test = array(array("calldate"=> "2013-08-14","src"=> "1002"),
              array("calldate"=> "2013-08-18","src"=> "1003")
        );

echo json_encode($test,JSON_FORCE_OBJECT); 
//Outputs :
//{"0":{"calldate":"2013-08-14","src":"1002"},"1":{"calldate":"2013-08-18","src":"1003"}}

and

echo json_encode($test);
//Outputs :
// Gave me [{"calldate":"2013-08-14","src":"1002"},{"calldate":"2013-08-18","src":"1003"}]

so using just

echo json_encode($dblist);

should do the trick

于 2013-09-05T14:28:21.467 回答