我想通过PHP和AngularJS获取MySQL数据库。
我做这个:
<?php
require_once "connect.php";
header('Content-Type: application/json');
$rows = array();
$conn = mysqli_connect($host, $user, $db_pass, $db_name);
$quer = mysqli_query($conn,"SELECT * FROM Sheet1");
while ($data = mysqli_fetch_assoc($quer)) {
$rows[] = $data;
}
echo json_encode($rows);
?>
但是什么也没有返回,当我将其设置为:
<?php
require_once "connect.php";
header('Content-Type: application/json');
$rows = array();
$conn = mysqli_connect($host, $user, $db_pass, $db_name);
$quer = mysqli_query($conn,"SELECT * FROM Sheet1");
while ($data = mysqli_fetch_assoc($quer)) {
$rows = $data;
}
echo json_encode($rows);
?>
我只会得到最后一条记录
我的主要html文件:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js'></script>
<title></title>
</head>
<body>
<div ng-app="App" ng-controller="mainController">
<ul ng-repeat=" x in data">
<li>{{x.id}}</li>
</ul>
</div>
<script src="mainController.js">
</script>
</body>
</html>
和我的Javascript文件:
var app = angular.module('App', []);
app.controller('mainController', function($scope, $http){
$http.get('database.php').then( function( response){
$scope.data = response.data;
});
});
请寻求帮助,这是我的第一次尝试。