3
<?php
    require 'dbinfo.php'; 
    try {
        $db = new PDO($dsn, $username, $password);
        $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
        $sth = $db->query("SELECT * FROM user_tracks");
        $loc = $sth->fetchAll();    
        $locations = array_values($loc);
        echo json_encode( array("user"=>( $locations )));
    } catch (Exception $e) {
      echo $e->getMessage();
    }
?>

The code should return:

{"user":[{"id":"1","Latitude":"12.9555033333","Longitude":"80.2461883333","Time":"06:32:57","Date":"2012-03-13","Speed":"0","Course":"183.92"},{...},{....}]}

when it is returning:

{"user":[{"id":"1","0":"1","Latitude":"12.9555033333","1":"12.9555033333","Longitude":"80.2461883333","2":"80.2461883333","Time":"06:32:57","3":"06:32:57","Date":"2012-03-13","4":"2012-03-13","Speed":"0","5":"0","Course":"183.92","6":"183.92"},{...},{....}]}

I'm unsure of what happening... Where is the problem here?

Thanks in advance!

1 Answer 1

8

fetchAll() returns both (note the 'fetch_style' argument comments/notes) string and numerically keyed data from the query results by default. If you want the string version only, you have to do

$loc = $sth->fetchAll( PDO::FETCH_CLASS );
Sign up to request clarification or add additional context in comments.

1 Comment

It works! Its the other way.. fetchAll(PDO::FETCH_CLASS); Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.