2011-12-22 13 views
6

Sto usando la versione 2.0.3 del codeigniter. Sto cercando di ottenere il numero di righe interessate dopo una query di aggiornamento utilizzando

$this->db->affected_rows 

Restituisce sempre 1, anche se nessuna riga è stata aggiornata. Ho provato con

mysql_affected_rows() 

e restituisce -1 per un errore di query e 0 se nessun record è stato aggiornato.

Modificare incluso il mio codice

sto solo usando

$country_id = $this->input->post('country_id'); 
$time=$this->input->post('time'); 

$insert_array = array(
    'country' => $this->input->post('name') 
); 
$this->db->update('country_master', $insert_array, array("country_id" => $country_id,"last_updated"=>$time)); 
$afftectedRows=$this->db->affected_rows(); 
+1

Ti dispiacerebbe mostrare il tuo codice? –

+0

che dipende dalla query che stai utilizzando prima $ this-> db-> affected_rows se puoi mostrare il tuo codice sarà facile condividere la soluzione. –

+0

Hmmm è stato un errore da parte mia.Il suo funziona bene ora – Nick

risposta

13

In realtà il mio codice è stato come questo

if (!$country_id) { 
    $this->db->insert('country_master', $insert_array); 
    $id = $this->db->insert_id(); 
} else { 
    $this->db->update('country_master', $insert_array, array("country_id" => $country_id, "last_updated" => $time)); 
} 
$afftectedRows = $this->db->affected_rows(); 

E ho modificato per

if (!$country_id) {     
    $this->db->insert('country_master', $insert_array); 
    $id = $this->db->insert_id(); 
} else { 
    $this->db->update('country_master', $insert_array, array("country_id" => $country_id, "last_updated" => $time)); 
    $afftectedRows = $this->db->affected_rows(); 
} 

E sta funzionando bene ora.

E grazie mille per le risposte ..

+1

Grazie, @Nick. Qualche tecnica per ottenere la riga interessata mentre si inseriscono più righe? –

Problemi correlati