2013-05-17 106 views
0

我覺得這很奇怪,但我似乎無法讓PHP報告錯誤。PHP將不會報告錯誤

我正在製作一個類來檢查和格式化CMS以外的某些結果,以便在不必創建自定義插件(由於時間限制)的情況下實現更高的過濾能力。

我在頁面頂部包含這兩行。

//error_reporting(E_ALL); 
//ini_set('display_errors', '1'); 

我已經切換每個並嘗試使用兩個,沒有效果。我的php.ini顯示display_errors與html_errors相同。 error_reporting設置爲1.

對於舊代碼,我有它的工作原理,錯誤報告顯示通知和錯誤。儘管在某些情況下我遇到了同樣的問題,但問題是由於在函數外部設置變量導致無法在函數內調用該函數而無需將其作爲函數參數。這就是爲什麼我決定改用班級來避免這個問題。

我想做一個簡單的類來做到這一點,並輸出結果爲JSON。我的PHP並不完美,但如果我至少得到一個錯誤,我可以輕鬆地診斷出錯的地方。在這種情況下,我無法讓PHP甚至顯示回聲,更不用說任何數據/錯誤。我得到的只是一張純白色的頁面。這裏是我正在使用的完整代碼,因爲我正在更新它的過程中,所以這是一團糟。

class k2JSON { 
    private $limit = ''; 
    private $featured = ''; 
    private $prefix = 'base_'; 
    private $filter_query = ''; 
    private $types = array(); 
    private $dates = array(); 
    private $build = array(); 
    private $db_arr = array(
     'name' => 'westerner_days', 
     'host' => 'localhost', 
     'user' => 'rem', 
     'pass' => '#w1n1nng' 
    ); 
    function __construct(){ 
     echo 'called constructor <br />'; 
     if(isset($_GET['limit'])){ 
      $this->limit = ' LIMIT '.$_GET['limit']; 
     } 
     if(isset($_GET['featured'])){ 
      $this->featured = " AND `featured`='".$_GET['featured']."'"; 
     } 
     $this->db = new mysqli(
      $db_arr['host'], 
      $db_arr['user'], 
      $db_arr['pass'], 
      $db_arr['name'] 
     ); 
     if(mysqli_connect_errno()){ 
      printf("Connect failed: %s\n", mysqli_connect_error()); 
      exit(); 
     } 
     $this->checkFilter(); 
     $this->checkDates(); 
     $this->checkExtra(); 
    }; 
    private function checkFilter(){ 
     if(isset($_GET['filter'])){ 
      switch($_GET['filter']){ 
       case 'entertainment': 
        $this->filter_query = "SELECT * FROM `{$this->prefix}k2_items` WHERE `catid` IN (5,6,7,8,9,10,11){$this->featured} AND `trash`='0'".$this->limit; 
        break; 
       case 'midway': 
        $this->filter_query = "SELECT * FROM `{$this->prefix}k2_items` WHERE `catid` IN (12,13,14){$this->featured} AND `trash`='0'".$this->limit; 
        break; 
       case 'off-site': 
        $this->filter_query = "SELECT * FROM `{$this->prefix}k2_items` WHERE `catid` IN (15,16,17,18){$this->featured} AND `trash`='0'".$this->limit; 
        break; 
       default: 
        $this->filter_query = "SELECT * FROM `{$this->prefix}k2_items` WHERE `catid` IN (5,6,7,8,9,10,11,12,13,14,15,16,17,18){$this->featured} AND `trash`='0'".$this->limit; 
        break; 
      } 
     }else{ 
      $this->filter_query = "SELECT * FROM `{$this->prefix}k2_items` WHERE `catid` IN (5,6,7,8,9,10,11,12,13,14,15,16,17,18){$this->featured} AND `trash`='0'".$this->limit; 
     } 
     $this->filter_result = $this->db->query($this->filter_query) or die($this->db->error.__LINE__); 
    }; 
    private function checkDates(){ 
     $this->date_query = "SELECT * FROM `{$this->prefix}k2_extra_fields` WHERE `id` IN (2,14,11) AND `published`='1'"; 
     $this->date_result = $this->db->query($this->date_query) or die($this->db->error.__LINE__); 
     while($row = $this->date_result->fetch_assoc()){ 
      $this->dates[$row['id']] = json_decode($row['value']); 
     } 
    }; 
    private function checkExtra(){ 
     $this->extra_query = "SELECT * FROM `{$this->prefix}k2_extra_fields` WHERE `published`='1'"; 
     $this->extra_result = $this->db->query($this->extra_query) or die($this->db->error.__LINE__); 
     while($row = $this->extra_result->fetch_assoc()){ 
      $this->types[$row['id']] = json_decode($row['value']); 
     } 
    }; 
    private function buildOutput($data){ 
     $compile = array(); 
     $compile['extra']= array(); 
     $compile['title'] = $data['title']; 
     $compile['featured'] = $data['featured']; 
     $compile['published'] = $data['published']; 
     $compile['desc'] = $data['introtext']; 
     $compile['link'] = 'http://'. 
      $_SERVER['HTTP_HOST']. 
      dirname($_SERVER['REQUEST_URI']).'/'. 
      (isset($_GET['filter'])? 
       $_GET['filter']: 
       in_array($data['catid'],[5,6,7,8,9,10,11])? 
        'entertainment': 
        in_array($data['catid'],[12,13,14])? 
         'midway': 
         in_array($data['catid'],[15,16,17,18])? 
          'off-site': 
          '' 
      ).'/view/item/'.$data['id'].'-'.$data['alias']; 
     $compile['type'] = $data['catid']; 
     $compile['image'] = array(
      'xsmall' => '/media/k2/items/cache/'.md5('Image'.$data['id']).'_XS.jpg', 
      'small' => '/media/k2/items/cache/'.md5('Image'.$data['id']).'_S.jpg', 
      'medium' => '/media/k2/items/cache/'.md5('Image'.$data['id']).'_M.jpg', 
      'large' => '/media/k2/items/cache/'.md5('Image'.$data['id']).'_L.jpg', 
      'xlarge' => '/media/k2/items/cache/'.md5('Image'.$data['id']).'_XL.jpg', 
     ); 
     $extra = json_decode($data['extra_fields']); 
     foreach($extra as $key=>$value){ 
      switch($extra[$key]->id){ 
       case 2: 
        if(is_array($extra[$key]->value)){ 
         $return_array = array(); 
         foreach($extra[$key]->value as $key2=>$value2){ 
          if($this->dates['2'][$extra[$key]->value[$key2]-1]->name!=null){ 
           $return_array[]=$this->dates['2'][$extra[$key]->value[$key2]-1]->name; 
          } 
         } 
         $compile['date'] = $return_array; 
        }else{ 
         $compile['date'] = $this->dates['2'][$extra[$key]->value-1]->name; 
        } 
        break; 
       case 14: 
        if(is_array($extra[$key]->value)){ 
         $return_array = array(); 
         foreach($extra[$key]->value as $key2=>$value2){ 
          if($dates['14'][$extra[$key]->value[$key2]-1]->name!=null){ 
           $return_array[]=$this->dates['14'][$extra[$key]->value[$key2]-1]->name; 
          } 
         } 
         $compile['date'] = $return_array; 
        }else{ 
         $compile['date'] = $this->dates['14'][$extra[$key]->value-1]->name; 
        } 
        break; 
       case 11: 
        if(is_array($extra[$key]->value)){ 
         $return_array = array(); 
         foreach($extra[$key]->value as $key2=>$value2){ 
          if($dates['11'][$extra[$key]->value[$key2]-1]->name!=null){ 
           $return_array[]=$this->dates['11'][$extra[$key]->value[$key2]-1]->name; 
          } 
         } 
         $compile['date'] = $return_array; 
        }else{ 
         $compile['date'] = $this->dates['11'][$extra[$key]->value-1]->name; 
        } 
        break; 
       case 1: 
        $compile['extra'][$extra[$key]->id] = $extra[$key]->value; 
        break; 
       case 15: 
        if(is_array($extra[$key]->value)){ 
         $return_array = array(); 
         foreach($extra[$key]->value as $key2=>$value2){ 
          $return_array[]=$this->types['15'][$extra[$key]->value[$key2]]->name; 
         } 
         $compile['extra'][$extra[$key]->id] = $return_array; 
        }else{ 
         $compile['extra'][$extra[$key]->id] = $this->types['15'][$extra[$key]->value]->name; 
        } 
        break; 
       case 1: 
        if(is_array($extra[$key]->value)){ 
         $return_array = array(); 
         foreach($extra[$key]->value as $key2=>$value2){ 
          $return_array[]=$this->types['1'][$extra[$key]->value[$key2]]->name; 
         } 
         $compile['extra'][$extra[$key]->id] = $return_array; 
        }else{ 
         $compile['extra'][$extra[$key]->id] = $this->types['1'][$extra[$key]->value]->name; 
        } 
        break; 
       case 7: 
        $compile['extra'][$extra[$key]->id] = $extra[$key]->value; 
        break; 
       case 18: 
        $compile['extra'][$extra[$key]->id] = $extra[$key]->value; 
        break; 
       case 10: 
        $compile['extra'][$extra[$key]->id] = $extra[$key]->value; 
        break; 
       case 4: 
        $compile['extra'][$extra[$key]->id] = $extra[$key]->value; 
        break; 
       case 16: 
        $compile['extra'][$extra[$key]->id] = $extra[$key]->value; 
        break; 
       case 12: 
        $compile['extra'][$extra[$key]->id] = $extra[$key]->value; 
        break; 
      } 
     } 
     return $compile; 
    }; 
    public function parse(){ 
     if($this->filter_result->num_rows > 0) { 
      while($row = $this->filter_result->fetch_assoc()) { 
       if(isset($_GET['date'])){ //date but could have type 
        $extra = json_decode($row['extra_fields']); 
        foreach($extra as $key=>$value){ 
         if($extra[$key]->id==2||$extra[$key]->id==14||$extra[$key]->id==11){ //find date extrafield 
          $check_array = array(); 
          foreach($extra[$key]->value as $key2=>$value2){ 
           $check_array[]=$dates['2'][$extra[$key]->value[$key2]-1]->name; 
          } 
          foreach($extra[$key]->value as $key2=>$value2){ 
           $check_array[]=$dates['14'][$extra[$key]->value[$key2]-1]->name; 
          } 
          foreach($extra[$key]->value as $key2=>$value2){ 
           $check_array[]=$dates['11'][$extra[$key]->value[$key2]-1]->name; 
          } 
          $compile['date'] = $check_array; 
          if(in_array($_GET['date'],$check_array)){ 
           if(isset($_GET['type'])){ //type is set 
            if($row['catid']==$_GET['type']){ 
             $this->build[]=$this->buildOutput($row); 
            } 
           }else{ //type is not set, output all values. 
            $bthis->build[]=$this->buildOutput($row); 
           } 
          } 
         } 
        } 
       }else if(isset($_GET['type'])){ //only type 
        if($row['catid']==$_GET['type']){ 
         $this->build[]=$this->buildOutput($row); 
        } 
       }else{ //no filters 
        $this->build[]=$this->buildOutput($row); 
       } 
      } 
     } 
    }; 
    public function outputResult(){ 
     print_r($this->build); 
     echo json_encode($this->build); 
    }; 
}; 
echo 'init <br />'; 
$json = new k2JSON(); 
$json->parse(); 
$json->outputResult(); 

我知道可能有一些非常明顯的錯誤。但沒有PHP錯誤,我無法縮小它。我希望對於有更多PHP經驗的人來說,這個問題有一個明顯的問題。

+0

當你說你正在包括這些行,我認爲你是從他們開始刪除//? – bhttoan

回答

1

你確定你收到錯誤了嗎?

嘗試,這也:

@ini_set('error_reporting', 'true'); 
error_reporting(E_ALL | E_STRICT); 

的功能可能是錯誤的,你寫的功能,如

public function something() {}; <== 

寫他們喜歡

public function something() {} 

犯規解決您的錯誤問題,但至少備註

+0

我已經嘗試過,我仍然只是得到一個白頁。是的,否則,因爲我的「回聲」聲明應該顯示正確的東西? –

+0

奇怪,因爲我得到錯誤,當我運行這個,沒有任何ini_set或error_reporting代碼... 解析錯誤:語法錯誤,意外';',期待在G:\ xampp \ htdocs \ test \ index.php T_FUNCTION行37 –

+0

是的,它讓我感到困惑,我覺得它可能是apache。儘管我的phpinfo顯示了我所看到的適當的錯誤設置。 –

0

刪除';'在類方法中使用方括號後,告訴我們它是否可用。

+0

我刪除了所有我可以看到的,仍然是白色的頁面。 –

+0

還有更多的錯誤,那麼這1也解析錯誤:語法錯誤,意想不到的'['在第85行的G:\ xampp \ htdocs \ test \ index.php –

+0

更改數組聲明,從5.4到舊方式你的PHP服務器5.4?):即行85 [5,6,7,..]陣列(5,6,7,...) – jgpATs2w

0

//error_reporting(E_ALL);

//ini_set('display_errors', '1');

退出 「//」 並刪除 「;」函數聲明後

+0

我曾表示,我切換他們,我的意思是我取消了他們的評論並以不同的變化評論他們進行測試。至於';'在實現功能之後,這不是最終的根本問題。這是PHP不允許頁面重寫error_reporting。我使用'''的原因首先是我主要是一名JavaScript程序員,並且最好避免以後壓縮代碼的問題。 –