Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i wish to display data from mysql dataBase using angularJS but all in vain. Pls review myu 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?

share|improve this question
 
Can you show the JSON data that is sent to the page? Perhaps PHP isn't outputting what you would expect. –  Alexander R Aug 22 '12 at 13:11

1 Answer

up vote 0 down vote accepted

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

share|improve this answer
 
my mistake :) i forgot to add [] and commas in the generated json –  z22 Aug 23 '12 at 9:52
 
I use Services_JSON and it has been flawless for me pear.php.net/pepr/pepr-proposal-show.php?id=198 –  Dan Doyon Aug 23 '12 at 18:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.