2011-04-15 60 views
0

我正在使用XAMPP 1.7.2(PHP 5.3)在winxp localhost上進行遷移。 有一個功能工作得很好。來自CodeIgniter模塊。未定義變量不同PHP版本之間的問題?

function get_cal_eventTitle($year, $month){ 
     $this->db->where('eventYear =', $year); 
     $this->db->where('eventMonth =', $month); 
     $this->db->select('eventTitle'); 
      $query = $this->db->get('tb_event_calendar'); 

     $result = $query->result(); 

     foreach ($result as $row) 
     { 
      $withEmptyTitle = $row->eventTitle;   
      //echo $withEmptyTitle.'<br>'; 
      $noEmptyTitle = str_replace(" ","%20",$withEmptyTitle); 
      $withUrlTitle = '<a href='.base_url().'index.php/calendar/singleEvent/'.$year.'/'.$month.'/'.$noEmptyTitle.'/'.'>'.$withEmptyTitle.'</a>';   
      //echo $withUrlTitle.'<br>'; 
      $row->eventTitle = $withUrlTitle;   
     } 
     return $result; 
    } 

當我上傳我的代碼到遠程服務器(PHP 5.2.9)。它顯示錯誤,因爲這,

withEmptyTitle未定義的變量

A PHP Error was encountered 
Severity: Notice 

Message: Undefined variable: withUrlTitle 

Filename: models/calendar_model.php 

Line Number: 54 // $withEmptyTitle = $row->eventTitle; 

但是當我啓用線路echo $withEmptyTitle.'<br>';註釋。它在遠程服務器上運行良好。

假設withEmptyTitle回聲四月運行事件在這裏。

我不知道爲什麼?你能否給我一些解決這個問題的建議?讚賞您的意見。

+1

您確定此代碼給出錯誤嗎?什麼是實際線路? – Khez 2011-04-15 11:59:26

+1

請注意,PHP 5。2正式脫離支持,所以如果可能的話,你的遠程服務器應該升級到5.3。即使你不能這樣做,它肯定會*升級到5.2.17,這是5.2的最後一個安全更新 - 你當前的版本5.2.9已經過時了,可能並不安全。 – Spudley 2011-04-15 11:59:38

+0

@Khez,是的,我在我的winxp – 2011-04-15 12:00:59

回答

1

你所看到的可能不是錯誤,而是警告

由於您使用的是尚未初始化的變量,PHP可能會發出警告。聽起來您的本地開發PHP安裝可能會抑制警告消息,而您的活動服務器已啓用它們。 (事實上​​,最好的做法是把它倒過來!)

在這種情況下,它可能是$withEmptyTitle = $row->eventTitle;未初始化的變量$withEmptyTitle,如果eventTitle屬性返回爲未設置。然後它會跟隨線並在嘗試使用str_replace()調用中的變量時發出警告。

你可能避免這樣的警告:

  • 開關警告消息關閉在php.ini
  • ini_set()程序時將其關閉。
  • 使用isset($withEmptyTitle)在實際使用之前檢查變量是否已設置。
  • 確保$row不實際包含的eventTitle屬性(在你的程序的情況下,如果缺少,它可能意味着錯誤數據或不正確的數據庫表設置?在任何情況下,你可以改變SQL查詢中使用IFNULL()或至少確保場被顯式查詢

[編輯]

我看到你編輯的問題我特別注意以下幾點:。

Line Number: 54 // $withEmptyTitle = $row->eventTitle; 

我注意到// ....是否意味着該行被註釋掉?你有沒有得到一個副本在服務器上有該行評論?那肯定會解釋你得到警告的事實!

+0

用於判斷源代碼中哪一行是否爲54。 – 2011-04-15 13:31:49