如何使用AngularJS和php显示数据

时间:2015-10-12 09:57:20

标签: php angularjs

我想将我的index.php页面与ngconnect.php连接,并希望显示我的表记录。

这是我的index.php页面.................

<html>
        <head>
            <title>first ng app recieve data from database</title>
            <script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js"></script>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
        </head>
        <body ng-app="myapp" ng-controller="myctrl">
            <ul>
              <li ng-repeat="x in names">
                {{ x }}
              </li>
            </ul>
            <script>
            $(function(){
                var app = angular.module('myapp',[]);
                app.controller('myctrl',function($scope,$http){
                    $http({method:'GET', url:'ngconnect.php'}).success(function(response){
                        $scope.names = response;
                    });
                });
            });
            </script>   
        </body>
    </html>

这是我的ngconnect.php页面.............

<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = mysql_connect($servername, $username, $password);
if (!$conn) {
    die("Connection failed: " . $conn->connect_error);
} else {
    $db_selected = mysql_select_db('dsc');  
    if (!$db_selected) {
        die ('Can\'t use dsc : ' . mysql_error());
    } else {
        $result = mysql_query('SELECT * from user');
        if (!$result) {
            die('Invalid query: ' . mysql_error());
        } else {
            $data = array();
            while ($row = mysql_fetch_array($result)) {          
                $data[] = $row;           
            }
            echo json_encode($data);
        }
    }
}
 mysql_close($conn);
?>
  

为什么我的数据没有显示?

ngconnect.php确实显示以下内容.....

[{"0":"1","user_id":"1","1":"Amit","first_name":"Amit","2":"","middle_name":"","3":"Kushwaha","last_name":"Kushwaha","4":"akushwaha.softvisionindia@gmail.com","email":"akushwaha.softvisionindia@gmail.com","5":"2","rate_dsc_2":"2","6":"3","rate_dsc_3":"3","7":"4","rate_token":"4","8":"e10adc3949ba59abbe56e057f20f883e","password":"e10adc3949ba59abbe56e057f20f883e","9":"Enabled","status":"Enabled","10":"8858234949","phone":"8858234949","11":"NULL","forget_link":"NULL","12":"00:00:00","forget_time":"00:00:00"},{"0":"2","user_id":"2","1":"Sandeep","first_name":"Sandeep","2":"","middle_name":"","3":"Tripathi","last_name":"Tripathi","4":"sandeep.softvisionindia@gmail.com","email":"sandeep.softvisionindia@gmail.com","5":"10","rate_dsc_2":"10","6":"20","rate_dsc_3":"20","7":"30","rate_token":"30","8":"e10adc3949ba59abbe56e057f20f883e","password":"e10adc3949ba59abbe56e057f20f883e","9":"Enabled","status":"Enabled","10":"9936882864","phone":"9936882864","11":"","forget_link":"","12":"00:00:00","forget_time":"00:00:00"}]

1 个答案:

答案 0 :(得分:0)

试试这些代码。希望它会帮助你..

我假设您的响应是json格式(对象数组),因为您发布了对php文件的响应。

WSADATA wsaData;
auto iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
    return 1;
}
SOCKET ConnectSocket;
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {

    WSACleanup();
    return 1;
}
sockaddr_in clientService;
clientService.sin_family = AF_INET;

clientService.sin_addr.s_addr = inet_addr(argv[1]);
clientService.sin_port = htons((unsigned short) atoi(argv[2]));
iResult = connect(ConnectSocket, (SOCKADDR *) & clientService, sizeof (clientService));
if (iResult == SOCKET_ERROR) {
    WSACleanup();
    return 1;
}
struct sockaddr_in sin;
int addrlen = sizeof(sin);
if(getsockname(ConnectSocket, (struct sockaddr *)&sin, &addrlen) == 0 &&
   sin.sin_family == AF_INET &&
   addrlen == sizeof(sin))
{
    char *ip = inet_ntoa(sin.sin_addr);
    std::cout<<ip<<std::endl;
}