<?php
$db = mysqli_connect('localhost','root','','dbname')
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM tablename";
$result = mysqli_query($db, $query);
header('Content-Type: applicatio`enter code here`n/json');
while($row = mysqli_fetch_row($result)){
echo json_encode($row);
}
?>
<script>
var app = angular.module('mainApp', []);
app.controller('ctrl', function($scope, $http) {
$http.get("file.php").then(function (response) {
var d = JSON.parse(response);
console.log(d);
$scope.answers = response.data.records;
});
});
</script>
Response :- ["1","santosh","1","9"]["2","chandan","2","9"] but in angularjs showing error SyntaxError: Unexpected token [
you need to collect your database results in one array, then encode it into json
$data = array();
while($row = mysqli_fetch_row($result)){
$data[] = $row;
}
echo json_encode($data);
and in your context , you may not need to directly use mysqli_fetch_all
which it's returns:
Returns an array of associative or numeric arrays holding result rows.
as follows:
$rows = mysqli_fetch_all($result);
echo json_encode($rows);