3

I wish to display data from mysql database using angularJS but all in vain. Please review my code and suggest me where am I going wrong ?

  <?php
    $host = "localhost"; 
    $user = ""; 
    $pass = ""; 
    $database = "abc";
    $con = mysql_connect($host,$user,$pass);
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully'; 
    mysql_select_db($database,$con);  
     //select
    $query = "SELECT * FROM `product`"; 
    $result = mysql_query($query) OR die(mysql_error()); 
    $arr = array();
    //now we turn the results into an array and loop through them. 
    while ($row = mysql_fetch_array($result)) { 
        $name = $row['name']; 
        $description = $row['description']; 
        //echo 'This is: ' . $name . ' ' . $description . "<br/>\n"
        $arr[] = $row;
    } 
    echo json_encode($arr);
?>

controllers.js

 function ProductListCtrl($scope,$http) {
    $http.get('php/connect.php').success(function(data) {
        $scope.products = data;
    });
    //$scope.orderProp = 'name';

}

index.html

    <html ng-app>
    <head>
        <script src="../angular-1.0.1.min.js"></script>
        <script src="js/controllers.js"></script>
    </head>
    <body>
    <ul ng:controller="ProductListCtrl">
        <li ng:repeat="product in products">
            {{product.name}}
        </li>
    </ul>
    </body>
</html>

When I run index.html, I get infinite list of bullets with no result displayed.

Where am I going wrong?

1
  • Can you show the JSON data that is sent to the page? Perhaps PHP isn't outputting what you would expect. Commented Aug 22, 2012 at 13:11

1 Answer 1

1

I agree with Alexander, if you can get JSON data from your browser console that would help.

I think you wanted to do something like this in your php code to get only name and description $arr[] = array('name' => $name, 'description' => $description); instead of $arr[] = $row;

You may be getting back an error from your php code in the form of html and $http is pulling this apart as if it were an array.

hope this helps

--dan

Sign up to request clarification or add additional context in comments.

3 Comments

my mistake :) i forgot to add [] and commas in the generated json
I use Services_JSON and it has been flawless for me pear.php.net/pepr/pepr-proposal-show.php?id=198
Followed your tutorial z22 and happened the same with me, a lot of bullets, what do you mean with forgot to add the [] and commas in the json? where did you put that? :(

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.