GY22
GY22

Reputation: 785

Pulling data from DB and showing it in Angular

I am new to angular and getting it to work with php so bear with me. I was successful in saving data to the db with php. Now i want to retrieve it and show it in a list. The data is coming back from the DB but its not showing in my view (html)

my html code is:

<!-- Show users from db -->
<div>
    <ul ng-init="get_user()">
        <li ng-repeat="user in userInfo track by $index">{{user.user_name}} + {{user.user_email}}</li>
    </ul>
</div>
<!-- Show users from db -->

The code of my app.js is this:

/** function to get info of user added in mysql referencing php **/
$scope.get_user = function() {
    $http.get('db.php?action=get_user').success(function(data)
    {
        $scope.userInfo = data;   
        console.log("You were succesfull in show user info"); 
        console.log(data);
    })
}

And finally my php code:

/**  Function to get a user   **/
function get_user() {    
    $qryGet = mysql_query('SELECT * from tblUser');

    echo ($qryGet);

    $data = array();
    while($rows = mysql_fetch_array($qryGet))
    {
        $data[] = array(
                    "id"            => $rows['id'],
                    "user_name"     => $rows['user_name'],
                    "user_email"    => $rows['user_email']
                    );
    }

    print_r(json_encode($data));

    return json_encode($data);  
}

When running it in the browser i get this in my console. (http://gyazo.com/4621faa287a041ed9f7f610552407e9f). Any clues are welcome

Upvotes: 1

Views: 193

Answers (1)

Mathew Berg
Mathew Berg

Reputation: 28750

That's because your data is not

[{"id":"1", "user_name":"Foo Bar" ... }]

It's:

Connected to the DBResource id #4[{"id":"1", "user_name":"Foo Bar" ... }]

You can see it in the console log. Remove the line that says

echo ($qrtGet);

In your php

Upvotes: 1

Related Questions