Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been fetching data from my MySQL database but I am getting a SyntaxError: Unexpected token {. So after that I outputted my php file to the window and all I see is this:

{"0":"1","id":"1","1":"John","name":"John","2":"Doe","lastName":"Doe","3":"22","‌​age":"22"}

So I fetch the data from my database en encode it to JSON. So its normal to have a colon in the beginning right?

My second question is why is the data encoded so weird to JSON? I have a database with an ID, name, lastName and age, but the output is different that it should be. Have I done the encoding wrong?

The code I am using is down below.

myFoodController.js

app.controller('myFoodController', ["$scope", "myFoodFactory", function($scope, myFoodFactory) {
myFoodFactory.getMyFoodData(function(data) {
    $scope.myFood = data;
});
}]);

myFoodFactory.js

app.factory('myFoodFactory', function($http){
return{
    getMyFoodData: function(successcb){
        $http({method:'GET', url:'php/connect.php'})
            .success(function(data){
                successcb(data);
            });

    }
};

});

connect.php

$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db('db_test') or die (mysql_error()); 

$query = "SELECT * FROM `test`";
$output = mysql_query($query,$con);

while( $row = mysql_fetch_array($output)){
echo json_encode($row);
}
share|improve this question
add comment

1 Answer

if you are using codiegniter then json array will be easy to create in a proper way. according to your code you can create an array with the fetched array like $arr= array( 'itm1' => itemval1, 'itm2' => itemval2, ) then your json will be proper.and your code will work fine.

share|improve this answer
    
Could you please be more specific? Where should I add that? –  Mike 2 days ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.