title: AngularJS with PHP and I want the response in JSON
body: <p>I want the response in JSON  but I get the wrong response. I am learning AngularJS. Please help me.</p>
  
  <p>I am using <code>fetch.php</code> code below:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
      $servername = "localhost";
      $username = "root";
      $password = "admin";
      $dbname = "ghanshyam";
      // Create connection
      $conn = new mysqli($servername, $username, $password,$dbname );
  
      // Check connection
      if ($conn-&gt;connect_error) {
          die("Connection failed: " . $conn-&gt;connect_error);
      } 
  
  
       $query = "SELECT * FROM `table`"; 
       $result = $conn-&gt;query($query);
              if ($result-&gt;num_rows &gt; 0) {
          // output data of each row
          while($row = $result-&gt;fetch_assoc()) {
             $r[] = $row;
          }
      } else {
          echo "0 results";
      }
  
      header('Content-Type: application/json');
  
      echo json_encode(($r));
  
      $conn-&gt;close();
  ?&gt;
  </code></pre>
  
  <p>and also using HTML code below</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"&gt;&lt;/script&gt;
  &lt;script src="comen.js"&gt;&lt;/script&gt;
  &lt;body&gt;
   &lt;div&gt;
       &lt;p&gt;hello&lt;/p&gt;
      &lt;table ng-app="myApp" ng-controller="myCtrl" &gt;
          &lt;tr ng-repeat="x in myWelcome track by $index"&gt;
              &lt;td&gt; {{ x.one}} &lt;/td&gt;
              &lt;td&gt; {{ x.two }} &lt;/td&gt;
              &lt;td&gt; {{ x.three }} &lt;/td&gt;
  
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  &lt;script&gt;
  var app = angular.module('myApp', []);
  app.controller('myCtrl', function($scope, $http) {
      $http.get("fetch.php")
      .success(function(response) {
          console.log(response.data);
          $scope.myWelcome = response.data;
      });
  });
  &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I get the response as shown below:</p>
  
  <pre><code>&lt;?php
  $servername = "localhost";
  $username = "root";
  $password = "admin";
  $dbname = "ghanshyam";
  // Create connection
  $conn = new mysqli($servername, $username, $password,$dbname );
  
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  } 
  
  
   $query = "SELECT * FROM `table`"; 
   $result = $conn-&gt;query($query);
          if ($result-&gt;num_rows &gt; 0) {
      // output data of each row
      while($row = $result-&gt;fetch_assoc()) {
         $r[] = $row;
      }
  } else {
      echo "0 results";
  }
  </code></pre>