无法显示保存在phpmyadmin数据库中的文件详细信息

时间:2016-08-02 12:08:45

标签: php mysql angularjs

我正在使用angular和php构建一个项目,我有一个“文件”表,我只能使用php上传文件。现在我正在尝试检索我的数据库中的所有文件详细信息。我在控制台中的错误是:

 angular.js:13550 SyntaxError: Unexpected token < in JSON at position 0
    at Object.parse (native)

有人可以查看我的代码吗?

Php用于显示:

    <?php header('Content-Type: text/html; charset=utf-8');
  $connection = mysqli_connect('localhost','root','','hamatkin');

  mysqli_query($connection,"SET character_set_client = utf8");
  mysqli_query($connection,"SET character_set_connection = utf8");
  mysqli_query($connection,"SET character_set_results = utf8");

  if(!$connection){
    die("couldnt connect".mysqli_error);
  }
  $query = "SELECT * FROM `file` ";
  $queryResult = $connection->query($query);

  $queryResult2 = array();
  if($queryResult === FALSE) { die($connection->error); }
  if( $queryResult->num_rows>0){
    while($row = $queryResult->fetch_assoc()){
      $queryResult2[] = $row;
    }
  }
  $queryResult3 = json_encode($queryResult2);
   echo json_encode($queryResult3);
?>

控制器:

"use strict";

angular.module('dataSystem').controller('allPriceOffersCtrl', function($scope,$route,$location,$http) {
  $http({method:'GET', url:'api/customers-tab/get-all-priceOffers.php/'})
      .then(function(response) {
        var arr = JSON.parse(JSON.parse(response.data));
        $scope.files = arr;
      })

      // This will log you the error code and trace, if there is an error.

      .catch(function(err) {
        console.log('err', err)
      });

});

HTML:

    <div class="table-responsive">
  <table  class="customer-list table table-striped">
    <thead>
      <tr>
        <th class="Column-Header">מספר</th>
        <th class="Column-Header">משו</th>
        <th class="Column-Header">שם מלא</th>
        <th class="Column-Header">ת.ז./עוסק מורשה</th>
        <th class="Column-Header">עיר</th>
        <th class="Column-Header">כתובת</th>
        </tr>
    </thead>
    <tbody>
      <tr ng-repeat="x in files">
        <!-- <td>{{$index + 1}}</td> -->
        <td>{{ x.id}}</td>
        <td>{{ x.name}}</td>
        <td>{{ x.mime}}</td>
        <td> {{ x.size}} </td>
        <td> {{ x.data}} </td>
        <td> {{ x.created}} </td>
        </tr>
    </tbody>
  </table>
</div>

1 个答案:

答案 0 :(得分:3)

调试不友好代码的典型案例。

首先,您必须检查$connection->query是否可能返回错误,这意味着返回值不是结果集而是false。所以

if($queryResult === false) {
    die($connection->error);
}

即便如此,你应该有一个“理智”的结果,这意味着你期望一个数组数组,所以$queryResult2至少应该被初始化为一个数组:

$queryResult2 = array(); // before the while loop.

另外:你绝对不需要检查num_rows,只需执行while循环,因为如果没有行,它就不会有任何错误。 fetch很聪明!

事实证明,正在排序的列date不存在。

经过长时间的调试后,我们发现,数据库中可能存在二进制数据,而json_encode()由于“畸形的utf8字符”而返回false。 angularjs部分必须适应不经常解码/解析json。我假设没有二进制数据存储在数据库中。这是一个警告,调试时没有任何假设。始终检查错误返回值。

调试友好代码有帮助。请在世界范围内编写调试友好的代码,因为这样做并不麻烦,调试就像将错误消息放入某个搜索引擎一样简单,而不是让stackoverflow在你的问题的评论中调试你的代码。 / p>