Ero Stefano Ero Stefano - 1 year ago 118
Javascript Question

AngularJS: get JSON from php script

I'm trying the following AngularJS tutorial at w3schools.

Link to w3schools tutorial

I changed the script to make it work with one of my php script:

<!DOCTYPE html>
<html >
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
table tr:nth-child(odd) {
background-color: #f1f1f1;
table tr:nth-child(even) {
background-color: #ffffff;
<script src=""></script>

<div ng-app="myApp" ng-controller="customersCtrl">

<tr ng-repeat="x in names">
<td>{{ }}</td>


var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
//my url
.then(function (response) {$scope.names =;});


This is actually not working. I guess its because of the php file, here is the code:

//Verbindung zur Datenbank
include "inc_mysql.php";

$select = "SELECT * FROM module ORDER BY name ASC";

// Codierung

//SQL-Befehl in Variable speichern
$sql = mysql_query($select);

//Array erstellen
$jsonArray = array();

while ($row = mysql_fetch_array($sql)){
$jsonArray[] = $row;

echo json_encode($jsonArray, JSON_UNESCAPED_UNICODE);

Do you figure out what is missing?

Here is the JSON output

[{"0":"12","id":"12","1":"Betriebsbuchhaltung","name":"Betriebsbuchhaltung","2":"2016-10-19 15:44:16","timestamp":"2016-10-19 15:44:16"},{"0":"13","id":"13","1":"Datenbanken","name":"Datenbanken","2":"2016-10-28 20:35:49","timestamp":"2016-10-28 20:35:49"},{"0":"6","id":"6","1":"Logistik","name":"Logistik","2":"0000-00-00 00:00:00","timestamp":"0000-00-00 00:00:00"},{"0":"14","id":"14","1":"Statistik","name":"Statistik","2":"2016-11-02 15:13:13","timestamp":"2016-11-02 15:13:13"},{"0":"1","id":"1","1":"System Engineering","name":"System Engineering","2":"0000-00-00 00:00:00","timestamp":"0000-00-00 00:00:00"}]

Answer Source

the returned json has no records property, here's a simple solution:

echo json_encode(["records"=>$jsonArray], JSON_UNESCAPED_UNICODE); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download