使用 php 从我的 SQL 数据库生成了一个 .json
文件。
我在前端使用 Angular,并使用 php 作为后端。
`
$query = "SELECT * FROM users";
$rows = array();
$result = mysqli_query($connection,$query);
while($r = mysqli_fetch_assoc($result)){
$rows[] = $r;
}
print json_encode($rows);
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($rows));
fclose($fp);
我需要执行 CRUD。我应该如何做才能让我的 .json
文件自动更新。?
最佳答案
您可以创建一个 change.php
或 update.php
文件,该文件在执行时会简单地覆盖服务器端的 .json
文件.
<?php
$data = json_decode(file_get_contents("php://input"),true);
file_put_contents('file.json', $data);
?>
现在,您可以通过 $http.get()
从服务器获取 file.json
并将其存储到您的 $scope 或 angularjs 中的任何其他变量中。
然后您可以对该变量执行 CRUD 操作。
您现在需要做的就是调用$http.post('URL/chage.php',YOUR_VARIABLE)
这只会用新内容覆盖您的 .json 文件。
因此,每当您请求从 .json
文件获取 json 时,您将始终获得更新的内容。
关于php - 使用 Angular 对 json 执行 CRUD 操作。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43405022/