1

I am trying to get data from MySQL using PHP and show them using AngularJS. But here I am getting the following message in browser's console and became unable to fetch the data.

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

Here is my code:

edit.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;
?>

update.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];
}

Here I need inside the success function(i.e-$http.get(..)) the console message should display all requested DB values. How can I resolve this issue?

5
  • perhaps $data is not a happy camper on line 3. dump it out Commented Sep 24, 2015 at 6:31
  • @Drew : I removed this but the output is not coming. Commented Sep 24, 2015 at 7:13
  • I suppose by "dump it out" @Drew means make a var_dump to see what's in the variable, like: var_dump($data);. Not remove it. See here var_dump Commented Sep 24, 2015 at 12:37
  • yes @xpy sorry bout that Commented Sep 24, 2015 at 12:43
  • follow this tutorial Angular with php mysql Commented May 9, 2016 at 12:16

2 Answers 2

0

success and other convenience methods are removed from $http. You need to use the promise-like API on $http like below

        // 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);
        });
Sign up to request clarification or add additional context in comments.

3 Comments

@ Arkantos : I added your line but it is showing the console output as responsefalse .
Did you check the actual response for that Ajax call in the Dev Tools (F12) Network tab ? what does it say ?
Then you need to have a look at your PHP script. We expect our PHP server to send some response for the GET request that we're making in AngularJS. I don't know PHP but check the answer from xpy and see if it helps in resolving your PHP issue.
0

php://input holds POST data so the $http.get('js/edit.php'...) request should be an $http.post('js/edit.php'...) request instead in order to make this work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.