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);
}