0

Hello, so I prettty much copied this code and it doesn't work for me, but does for other people (the entire html and php) When I try the query in SQL Management it works, so I don't think it's the database When I try to read from the database it's just blank, no errors in the console Here is my select.php:

<?php  
 //select.php  
 $connect = mysqli_connect("den1.mssql5.gear.host", "testiranje", "nijebitno@", "testiranje");  
 $output = array();  
 $query = "SELECT * FROM dbo.tbl_user";  
 $result = mysqli_query($connect, $query);  
 if(mysqli_num_rows($result) > 0)  
 {  
      while($row = mysqli_fetch_array($result))  
      {  
           $output[] = $row;  
      }  
      echo json_encode($output);  
 }  
 ?>  

And here is my index.html:

<!DOCTYPE html>  
<!-- index.php !-->  
<html>  
     <head>  
          <title>Webslesson Tutorial | AngularJS Tutorial with PHP - Fetch / Select Data from Mysql Database</title>  
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
          <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
     </head>  
     <body>  
          <br /><br />  
          <div class="container" style="width:500px;">  
               <h3 align="center">AngularJS Tutorial with PHP - Fetch / Select Data from Mysql Database</h3>  
               <div ng-app="myapp" ng-controller="usercontroller" ng-init="displayData()">  
                    <label>First Name</label>  
                    <input type="text" name="first_name" ng-model="firstname" class="form-control" />  
                    <br />  
                    <label>Last Name</label>  
                    <input type="text" name="last_name" ng-model="lastname" class="form-control" />  
                    <br />  
                    <input type="submit" name="btnInsert" class="btn btn-info" ng-click="insertData()" value="ADD"/>  
                    <br /><br />  
                    <table class="table table-bordered">  
                         <tr>  
                              <th>First Name</th>  
                              <th>Last Name</th>  
                         </tr>  
                         <tr ng-repeat="x in names">  
                              <td>{{x.first_name}}</td>  
                              <td>{{x.last_name}}</td>  
                         </tr>  
                    </table>  
               </div>  
          </div>  
     </body>  
</html>  
<script>  
var app = angular.module("myapp",[]);  
app.controller("usercontroller", function($scope, $http){  
     $scope.insertData = function(){  
          $http.post(  
               "insert.php",  
               {'firstname':$scope.firstname, 'lastname':$scope.lastname}  
          ).success(function(data){  
               alert(data);  
               $scope.firstname = null;  
               $scope.lastname = null;  
               $scope.displayData();  
          });  
     }  
     $scope.displayData = function(){ 
         $scope.names = new Array;
          $http.get("select.php")  
          .success(function(data){  
        console.log("sranje");
            $scope.names = data;  
            console.log(data);

          });  
     }  
});  
</script>  
Drason
  • 41
  • 1
  • 8
  • you should consider rewriting the code to use `.this` instead of `.success`. see https://stackoverflow.com/q/35329384/2495283 – Claies May 27 '18 at 00:21

1 Answers1

0

You can try adding

header('Content-type: application/json');

before your echo in php function. In this way you "tell" that your php function return json data.

<?php  
 //select.php  
 $connect = mysqli_connect("den1.mssql5.gear.host", "testiranje", "nijebitno@", "testiranje");  
 $output = array();  
 $query = "SELECT * FROM dbo.tbl_user";  
 $result = mysqli_query($connect, $query);  
 if(mysqli_num_rows($result) > 0)  
 {  
      while($row = mysqli_fetch_array($result))  
      {  
           $output[] = $row;  
      }
      header('Content-type: application/json');
      echo json_encode($output);  
 }  
 ?>