2015-05-06 80 views
1

後,我使用CodeIgniter的PHP框架着手,PHP函數甚至返回

在我的模型,我有這樣的功能(簡體):

public function do_stuff($id=FALSE) 
{ 
    // Get all rows from `Table A` 
    $result_table_a = $this->db->get('table_a')->result_array(); 

    // Update `Table B` data 
    $update_table_b = $this->db->update('table_b', $data); 

    // If failed to update `Table B` return FALSE 
    if (!$update_table_b || $this->db->affected_rows()<1) 
    { 
     $this->data['message'] = '<p>Failed to Update (#34)!</p>'; 
     return FALSE; 
    } 

    // Update all selected rows from `Table A` 
    foreach ($result_table_a AS $result) 
    { 
     $this->function_to_update_table_a($some_data); 
    } 
} 

今天我跑這個功能作爲用戶,我看到錯誤Failed to Update (#34)!,但後來我注意到foreach循環也運行,其內部的功能已更新table_a以及

這可能嗎?返回後可以繼續工作嗎?

我真的很困惑

+1

這意味着你永遠不會到達你的退貨聲明。你需要弄清楚爲什麼。 –

+0

嘗試調試你的代碼.. –

+0

@JohnConde但是,那麼錯誤是如何顯示的? – Vladimir

回答

2

檢查是否受到影響任何行並不以確定是否有更新成功的正確方法。這是因爲如果您嘗試使用相同的值更新它,則表示沒有行被更新。

我會認爲$this->db->update()返回一個布爾值,因此如果你只是從if (!$update_table_b || $this->db->affected_rows()<1)改變你的if語句來此if (!$update_table_b)這應該解決您的問題。

+0

Augwa,現在我學到了很多東西,所以值得問這個問題甚至不是問題,謝謝 – Vladimir

相關問題