簡體   English   中英

無法顯示phpmyadmin數據庫中保存的文件詳細信息

[英]can't display files details that are saved in phpmyadmin database

我正在用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>

調試不友好代碼的典型情況。

首先,您必須檢查$connection->query可能返回錯誤,這意味着返回值不是結果集,而是false 所以

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

即使那樣,您也應該有一個“理智”的結果,也就是說,您期望有一個數組數組,因此至少$queryResult2應該初始化為數組:

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

另外:您絕對不需要檢查num_rows ,只需執行while循環,因為如果沒有行,它將不會做任何錯誤。 fetch是那么聰明!

事實證明,正在排序的列date不存在。

經過長時間的調試之后,我們發現數據庫中可能存儲了二進制數據,並且由於“格式錯誤的utf8字符”, json_encode()返回了false。 angularjs部分必須調整為不經常解碼/解析json。 我當時假設數據庫中沒有存儲二進制數據。 將此作為警告,調試時沒有任何假設。 始終檢查錯誤返回值。

調試友好的代碼有幫助。 請全世界,編寫易於調試的代碼,因為這樣做並不麻煩,調試變得像將錯誤消息放入某個搜索引擎一樣容易,並且不要讓stackoverflow在問題注釋中調試您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM