2012-03-15 7 views
15

En Ci, tengo la siguiente función. ¿Cómo pruebo que la consulta se insertó correctamente sin error?Cómo probar si Ci insertó datos con éxito

public function postToWall() { 
    $entryData = $this->input->post('entryData'); 
    $myChurchId = $this->session->userdata("myChurchId"); 
    $this->db->query("INSERT IGNORE INTO wallPosts (entryData, entryCreationDateTime, wpChurchId) 
         VALUES('$entryData', NOW(), '$myChurchId')"); 
} 

Respuesta

45

Puede utilizar $this->db->affected_rows() función de CodeIgniter.

Ver más información here

Usted puede hacer algo como esto:

return ($this->db->affected_rows() != 1) ? false : true; 
+0

gracias hombre! Esta fue una gran ayuda. –

+10

O simplemente: 'return $ this-> db-> affected_rows()> 0;' – harrison4

3

También puede hacerlo utilizando Transacciones como esto:

  $this->db->trans_start(); 
      $this->db->query("INSERT IGNORE INTO wallPosts (entryData, entryCreationDateTime, wpChurchId) 
         VALUES('$entryData', NOW(), '$myChurchId')"); 
      $this->db->trans_complete(); 

      if ($this->db->trans_status() === FALSE) { 
       return "Query Failed"; 
      } else { 
       // do whatever you want to do on query success 
      } 

Aquí está more info a las Transacciones en CodeIgniter!

Cuestiones relacionadas