php - 无法使用 AngularJS 和 PHP 从数据库中获取数据

标签 php mysql angularjs

我正在尝试使用 PHP 从 MySQL 获取数据并使用 AngularJS 显示它们。但是我在浏览器的控制台中收到以下消息并且无法获取数据。

response <br />
<b>Notice</b>:  Trying to get property of non-object in <b>C:\xampp\htdocs\phpdemo\js\edit.php</b> on line <b>3</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>C:\xampp\htdocs\phpdemo\js\edit.php</b> on line <b>9</b><br />
false

这是我的代码:

编辑.php:

<?php
$data = json_decode(file_get_contents("php://input"));
$user_id = mysql_real_escape_string($data->user_id);
$con = mysql_connect('localhost', 'root', '');
mysql_select_db('dbtuts', $con);
$qry="SELECT * FROM users WHERE user_id=".$user_id;
$qry_res = mysql_query($qry);
$r = array();
if( $qry_res->num_rows>0){
while($row = $qry_res->fetch_assoc()){
$r[] = $row;
}
}
$res = json_encode($qry_res);
echo $res;
?>

更新.js:

var app=angular.module("edit_data", []);
app.controller("updateController",function($scope,$http,$location){
    $scope.errors = [];
    $scope.msgs = [];
    var id=gup( "edt_id" );
    console.log("id is :",id);
    $http.get('js/edit.php',{"user_id":id}).success(function(response){
        console.log('response',response);
    });
    $scope.update_data=function(){
        $http.post('js/update.php',{"first_name":$scope.first_name,"last_name":$scope.last_name,"city":$scope.city}
        ).success(function(data, status, headers, config){
            if(data.msg!=''){
            $scope.msgs.push(data.msg);
            }else{
                $scope.errors.push(data.error);
            }
        }).error(function(data, status) { // called asynchronously if an error occurs
               // or server returns response with an error status.
              $scope.errors.push(status);
        });
    }
});
function gup( name ) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

这里我需要在成功函数(i.e-$http.get(..))中,控制台消息应该显示所有请求的数据库值。我该如何解决这个问题?

最佳答案

success 和其他方便的方法已从 $http 中删除。您需要像下面这样在 $http 上使用类似 promise 的 API

        // this will fire a call to js/edit.php?user_id=value
        $http.get('js/edit.php',{"user_id":id})
             .then(function(response){
              // data property of response object will have actual response from server
              console.log('response'+response.data);
        });

关于php - 无法使用 AngularJS 和 PHP 从数据库中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32754530/

相关文章:

javascript - AngularJS + RequireJS [错误注入(inject)]

PHP SHA3 功能

php - SQL自动递增同一张表多次

php - MySQL使用多个条件在单个查询中批量查询更新

mysql - 选择mysql中所有带有ascii代码的列

javascript - 如何在 AngularJS 自定义指令中指定模型?

javascript - 列出 (Angular.js) 中的对象

php - codeigniter 管理面板自动创建页面

php - 在当前命名空间中通过字符串实例化类

android - 如何在android ListView 中使用notifyDataSetAdapter?