2017-08-01 67 views
0

我需要簡單的Web服務來從本地數據庫獲取數據,但是當我將文件從測試服務器移動到實際服務器時,我只是收到空白頁左上角。 我的PHP代碼非常簡單:我的服務器返回空白頁在角落'null'

<?php 
// Include config 
require_once 'config/db.php'; 
$sql = new db(); 
$conn = $sql->connect(); 

$task = isset($_GET['task']) ? mysql_real_escape_string($_GET['task']) : ""; 

if(!empty($task)) 
{ 
    if($task === "totals") 
    { 
     $qur = mysql_query("working query - no problem here;"); 
     $result =array(); 
     while($r = mysql_fetch_array($qur)) 
     { 
      extract($r); 
      $result[] = array("total_value" => $total_value, 'orders_date' => $orders_date, 'number_of_orders' => $number_of_orders); 
     } 
     $json = $result; 
    } 
} 

@mysql_close($conn); 

/* Output header */ 
header('Content-type: application/json'); 
echo json_encode($json, JSON_PRETTY_PRINT); 
?> 

這裏是db.php中代碼:

<?php 
class db 
{ 
    // Properties 
    private $dbhost = 'ip-address'; 
    private $dbuser = 'xxx'; 
    private $dbpass = 'yyy'; 
    private $dbname = 'zzz'; 
    // Connect 
    public function connect() 
    { 
     $conn = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass); 
     mysql_select_db($this->dbname, $conn); 
    } 
} 
?> 
+0

mysql函數折舊。改用mysqli函數。還查找參數化查詢以避免SQL注入。 –

+0

我認爲這行'''echo json_encode ...'''什麼是生成頁面上的「null」。當你查看源代碼時,你看到了這一切嗎? –

+0

打開錯誤報告並查看正在生成的錯誤以及代碼中的位置。 –

回答

0

可能Json_encode沒有給予正確的答案。

1)另請確保JSON_PRETTY_PRINT僅適用於PHP版本> = 5.4。它的值是128,所以嘗試在頁面頂部只是在錯誤報告用128

更換JSON_PRETTY_PRINT調試

2)此外,以確保一次嘗試在編碼之前打印$json

3)正如評論中所提到的,只是在您實際獲取數據時才進行編碼

!empty($json){ 
header('Content-type: application/json'); 
echo json_encode($json, JSON_PRETTY_PRINT); 
}