不显示我的json

时间:2018-07-25 05:38:53

标签: php arrays json mysqli

当我运行此代码时,什么都没有发生,我想通过json显示它        

  include 'connect.php';

   $con = mysqli_connect($servername,$username,$password,$dbname);

    if (mysqli_connect_errno())
   {
     echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

     $query = "SELECT 
               firts_name,last_name,middle_name,path_to_photo_thumbnail FROM 
     CUSTOMERS";
        $result = mysqli_query($con,$query);

      $rows = array();
      if($result){
              while($r = mysqli_fetch_array($result))
              {
                $rows[] = $r;
              }
       print(json_encode($rows));
        }
      mysqli_close($con);
    ?>

以便我可以在android studio中阅读

1 个答案:

答案 0 :(得分:0)

尝试一下。检查您的查询是否有错误。添加了or die(mysqli_error($con));

include 'connect.php';

   $con = mysqli_connect($servername,$username,$password,$dbname);

    if (mysqli_connect_errno())
   {
     echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

     $query = "SELECT 
               first_name,last_name,middle_name,path_to_photo_thumbnail FROM 
     CUSTOMERS";
        $result = mysqli_query($con,$query) or die(mysqli_error($con));

      $rows = array();
      if($result){
              while($r = mysqli_fetch_assoc($result))
              {
                $fetch = [
                    'first_name' => $r['first_name'],
                    'last_name' => $r['last_name'],
                    'middle_name' => $r['middle_name'],
                    'path_to_photo_thumbnail' => $r['path_to_photo_thumbnail'],
                ];
                $rows[] = $fetch;
              }

        }
      mysqli_close($con);
     echo json_encode($rows);
    ?>