2014-01-26 40 views
1

我在Codeigniter中遇到了一個奇怪的問題。我運行一個查詢,更新了行:更新查詢沒有更新相應的行

function set_counter($key, $value){ 
    echo "$key : $value => "; 
    $this->db->query("UPDATE counter SET counter.`value` = ? WHERE counter.`key` = ?", array($value, $key)); 
    return $this->db->affected_rows(); 
} 

基準測試顯示,這個函數被調用和查詢不運行:

UPDATE counter SET counter.`value` = 1 WHERE counter.`key` = 'left_banner_start_id' 

該函數返回「1」,這意味着1行得到更新。

但在實際的行不在數據庫得到更新,value列始終「0」

任何解決什麼,我做錯了什麼?

編輯:

的問題變得更加古怪,當我退出()我的函數,該行實際上得到更新,否則它不會:

function set_counter($key, $value){ 
    echo "$key : $value - "; 
    $this->db->query("UPDATE counter SET counter.`value` = ? WHERE counter.`key` = ?", array($value, $key)); 
    //print_r($this->db->affected_rows()); 
    exit(); 
} 

編輯:

好吧,如果我在system/core/Output.php中退出()執行,它就可以工作:

// -------------------------------------------------------------------- 

    // Does the controller contain a function named _output()? 
    // If so send the output there. Otherwise, echo it. 
    if (method_exists($CI, '_output')) 
    { 
     $CI->_output($output); 
    } 
    else 
    { 
     exit(); 
     echo $output; // Send it to the browser! 
    } 

,但如果我退出()呼應$輸出後還不行:

// -------------------------------------------------------------------- 

    // Does the controller contain a function named _output()? 
    // If so send the output there. Otherwise, echo it. 
    if (method_exists($CI, '_output')) 
    { 
     $CI->_output($output); 
    } 
    else 
    { 
     echo $output; // Send it to the browser! 
     exit(); 
    } 

編輯:

我認爲有這條線這是防止它:

<img src="<?php echo base_url().$recent_ad['photo_thumb']; ?>" width="28" height="27" /> 

這個在另一個視圖:

<img src="<?php echo base_url().$featured_agent['photo_medium']; ?>" width="84" height="84" /> 

這裏photo_thumb和photo_medium是數據庫中另一個表中的列,它們具有空值。我填充了列,現在它工作正常。

但是爲什麼?是什麼原因?

+0

它是否在函數中打印正確的值? –

+0

是的。一切安好。即使codeigniter顯示查詢運行沒有錯誤,並顯示1行已在db中更新。但實際上沒有更新。 – younis

+0

我認爲根據echo「$ key:$ value =>」;不打印價值 –

回答

0

您是否嘗試將活動記錄語法的代碼切換?

function set_counter($key, $value){ 
    $this->db->where('key', $key)->update('counter', array('value' => $value)); 
    return $this->db->affected_rows(); 
} 
+0

是的。仍然沒有線索。 – younis

+0

然後,也許你可以告訴我們這個方法是從哪裏調用的。問題在於你面臨的問題不會是CI引擎上的任何問題,因爲這是一個常見的更新查詢。最有可能的是一個無效值。其實你說過photo_thumb和photo_medium是其他表格檢索的數據。有沒有可能你正在做一個INNER JOIN,將這些值關聯起來,並通過這個查詢獲得counter.key值?因爲那樣的話,如果你正在做一個null的INNER,那就沒有關鍵。 –

+0

這就是那麼煩人的事情。 photo_thumb和photo_medium完全無關。查詢不必對這些列甚至表格執行任何操作。 不,$ key和$ value是有效的字段(並且不使用內部連接),我告訴codeigniter確實顯示查詢已成功運行(但數據沒有更新) 這是基準測試得出的結果: 'UPDATE計數器SET counter.value = 1 WHERE counter.key ='left_banner_start_id''因此查詢中沒有錯誤。 – younis