php - 使用 Angular 对 json 执行 CRUD 操作。?

标签 php mysql angularjs json

使用 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.phpupdate.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/

相关文章:

javascript - 如何将同一文件夹的图标显示到另一个页面(链接)

php - PHP中的stream_socket_server是否用于创建生产服务器?

php - Laravel 使用自定义验证消息

javascript - 使用 Nodejs w/Express 将表单数据发布到 MySQL

python - SQL 参数的动态长度

angularjs - 广播模式的改变

php - 如何从不同的表中回显值?

mysql - 用户能否在交易完成之前读取数据

angularjs - Angular 资源在 For/While 循环中依次保存

javascript - 如何以 Angular 2制作 "loading"屏幕