title: Passing a MySQL query result to AngularJS through php
body: <p>I need to count a table in a mysql database, and for this I need to use php. I use the following code, <code>counttable.php</code>: </p>
  
  <pre><code>$query = "SELECT COUNT(*) FROM table";
  $result = mysqli_query($connection, $query);
  
  $count = (string)$result;
  echo $count;
  
  mysqli_close();
  </code></pre>
  
  <p>My plan is then to use the following Angular code to fetch the result:</p>
  
  <pre><code>myApp.factory("count", function($http) {
      return $http.get('counttable.php');
  });
  </code></pre>
  
  <p>In my controller I use the <code>success</code> and <code>error</code> functions of the factory to control the result.</p>
  
  <pre><code>count.success(function(data) {
      $scope.data = data;
  });
  
  count.error(function(data) {
      console.log("Count failed.");
  });
  </code></pre>
  
  <p>When I run my webpage, it will say "Count failed." in the console.
  I have tried encapsulating the php <code>$count</code> variable as JSON with <code>json_encode</code>, but this only gave me an empty object. I am guessing there might be a simple answer to this, but I just cant see it. </p>
  
  <p>Please help, any answer is highly appreciated!
  Thanks.</p>