使用$ http.get从sql数据库中检索数据

时间:2016-10-15 14:57:30

标签: php jquery mysql angularjs ajax

这是我之前post的延续。 我使用AngularJS的路由来加载不同的html文件。 对于学生显示,使用$ http.get从SQL数据库中检索数据。

我想检查我的ajax.php是否真的有效。 从浏览器开始运行时localhost/ajax.php 然后可以看到信息

        MySQL host info: 
    [{"0":"1","id":"1","1":"Mark","Name":"Mark","2":"Male","Gender":"Male","3":"London","City":"London"},
    {"0":"2","id":"2","1":"John","Name":"John","2":"Male","Gender":"Male","3":"Chenni","City":"Chenni"},
    {"0":"3","id":"3","1":"Hint","Name":"Hint","2":"Male","Gender":"Male","3":"Singapore","City":"Singapore"},
    {"0":"4","id":"4","1":"Sara","Name":"Sara","2":"Female","Gender":"Female","3":"Sydney","City":"Sydney"},
    {"0":"5","id":"5","1":"Tom","Name":"Tom","2":"Male","Gender":"Male","3":"New York","City":"New York"},
    {"0":"6","id":"6","1":"Pam","Name":"Pam","2":"Male","Gender":"Male","3":"Los Angeles","City":"Los Angeles"},

    {"0":"7","id":"7","1":"Catherine","Name":"Catherine","2":"Female","Gender":"Female","3":"Chicago","City":"Chicago"},
    {"0":"8","id":"8","1":"Mary","Name":"Mary","2":"Femal","Gender":"Femal","3":"Houston","City":"Houston"},

    {"0":"9","id":"9","1":"Mike","Name":"Mike","2":"Male","Gender":"Male","3":"Phoenix","City":"Phoenix"},
    {"0":"10","id":"10","1":"Rosie","Name":"Rosie","2":"Female","Gender":"Female","3":"Manchestor","City":"Manchestor"},
    {"0":"11","id":"11","1":"Lim","Name":"Lim","2":"Male","Gender":"Male","3":"Singapore","City":"Singapore"},
    {"0":"12","id":"12","1":"Tony","Name":"Tony","2":"Male","Gender":"Male","3":"Hong Kong","City":"Hong Kong"},
    {"0":"13","id":"13","1":"Royce","Name":"Royce","2":"Male","Gender":"Male","3":"London","City":"London"},
    {"0":"14","id":"14","1":"Hitler","Name":"Hitler","2":"Male","Gender":"Male","3":"Germany","City":"Germany"},
    {"0":"15","id":"15","1":"Tommy","Name":"Tommy","2":"Male","Gender":"Male","3":"New Jersy","City":"New Jersy"}]

看起来很正确。 我的数据库有以下数据。

id  Name    Gender  City
1   Mark    Male    London
2   John    Male    Chenni
3   Hint    Male    Singapore
4   Sara    Female  Sydney
5   Tom     Male    New York
6   Pam     Male    Los Angeles
7   Catherine   Female  Chicago
8   Mary    Femal   Houston
9   Mike    Male    Phoenix
10  Rosie   Female  Manchestor
11  Lim     Male    Singapore
12  Tony    Male    Hong Kong
13  Royce   Male    London
14  Hitler  Male    Germany
15  Tommy   Male    New Jersy

我的疑问是

如何以其他更好的替代方式调试我的ajax.php?我使用Netbeans IDE。 我在学生链接显示数据时遇到问题。 名称不会显示。如果ajax.php没有问题,哪里可能有问题?

由于

编辑: 我将代码更改为

connect.php

<?php
    // db credentials
    define('DB_HOST', 'localhost');
    define('DB_USER','root');
    define('DB_PASS','nyan');
    define('DB_NAME','Students');

    // Connect with the database.
    function connect()
    {
      $connect = mysqli_connect(DB_HOST ,DB_USER ,DB_PASS ,DB_NAME);

      if (mysqli_connect_errno($connect))
      {
        die("Failed to connect:" . mysqli_connect_error());
      }

      mysqli_set_charset($connect, "utf8");


      return $connect;
    }


?>

Ajax.php

<?php
    require 'connect.php';

    $connect = connect();

    // Get the data
    $students = array();
    $sql = "SELECT id, Name, Gender, City  FROM tblStudents";

    if($result = mysqli_query($connect,$sql))
    {
      $count = mysqli_num_rows($result);

      $cr = 0;
      while($row = mysqli_fetch_assoc($result))
      {
          $students[$cr]['id']    = $row['id'];
          $students[$cr]['Name']  = $row['Name'];
          $students[$cr]['Gender'] = $row['Gender'];
          $students[$cr]['City'] = $row['City'];         
          $cr++;
      }
    }

    $json = json_encode($students);
    echo $json;
    exit;

?>

的script.js

var app = angular.module("Demo", ["ngRoute"])
                 .config(function($routeProvider){
                 $routeProvider
                 .when("/home", {
                     templateUrl:"Templates/home.html",
                     controller:"homeController"
                 })
                 .when("/courses", {
                     templateUrl:"Templates/courses.html",
                     controller:"coursesController"
                 })
                 .when("/students", {
                     templateUrl:"Templates/students.html",
                     controller:"studentsController"
                 })
            })
            .controller("homeController", function($scope){
                 $scope.message = "Home Page";
            })
            .controller("coursesController", function($scope){
                 $scope.courses = ["C#", "VB.NET", "SQL Server", "ASP.NET"];
            })
            .controller("studentsController", function ($scope, $http) {
                 $http.get("ajax.php") .then(function(response) {
                    $scope.students = response;
                 });

            });

看起来没问题,但是当我按下F12时Chrome仍然出错。

GET http://localhost:8383/favicon.ico net::ERR_EMPTY_RESPONSE

我测试了Ajax.php和connect.php到/ var / www / html文件夹并运行localhost / Ajax.php 返回的数据是正确的,如下所示。

[{"id":"1","Name":"Mark","Gender":"Male","City":"London"},{"id":"2","Name":"John","Gender":"Male","City":"Chenni"},{"id":"3","Name":"Hint","Gender":"Male","City":"Singapore"},{"id":"4","Name":"Sara","Gender":"Female","City":"Sydney"},{"id":"5","Name":"Tom","Gender":"Male","City":"New York"},{"id":"6","Name":"Pam","Gender":"Male","City":"Los Angeles"},{"id":"7","Name":"Catherine","Gender":"Female","City":"Chicago"},{"id":"8","Name":"Mary","Gender":"Femal","City":"Houston"},{"id":"9","Name":"Mike","Gender":"Male","City":"Phoenix"},{"id":"10","Name":"Rosie","Gender":"Female","City":"Manchestor"},{"id":"11","Name":"Lim","Gender":"Male","City":"Singapore"},{"id":"12","Name":"Tony","Gender":"Male","City":"Hong Kong"},{"id":"13","Name":"Royce","Gender":"Male","City":"London"},{"id":"14","Name":"Hitler","Gender":"Male","City":"Germany"},{"id":"15","Name":"Tommy","Gender":"Male","City":"New Jersy"}]

所以Ajax.php和connect.php正在运行。只是AngularJs脚本无法正确检索。我的Script.js有什么问题?

由于

1 个答案:

答案 0 :(得分:0)

我有一个解决方案here。最后它与我的持续攻击有关。感谢大家的帮助和建议。