Welcome Guest, Not a member yet? Register   Sign In
database write failures
#1

[eluser]chubbypama[/eluser]
hi there. i'm just new to CI ... and i'm basing a mini application on some of the stuff i've learned from the CI tutorial.

i have written some code to write data from the URL into a database.
if the insert method fails, what's the best way to handle it?
for example, in my model, i have the following line of code to do my insert:
Code:
return $this->db->insert('clienthistory',$data);

what will be returned if the database gives some sort of error?
is there any best practices as far as handling errors are concerned? I've posted my code below. if you have any suggestions for improvement, i'm all ears.
thanks.

Controller:

Code:
public function saveClientXferStatus()
{
    //if no client ip avail, don't bother.
    if(isSet($_SERVER['REMOTE_ADDR']))
    {
      
        //NB:  URI Class is initialized automatically by the system.
        $val1=$this->uri->segment(0);
        $val2=$this->uri->segment(1);
        $val3=$this->uri->segment(2);

        //validate the data
        // If the values are not numeric there's nothing for us to do
        if ( ! is_numeric($val1)) AND ( ! is_numeric($val2) )  AND ( ! is_numeric($val3) )
        {                                                
            //write record to db.
            $this->xferLogger_model->save_Client_History($val1,$val2,$val3);
            $this->load->view('xferLogger/success');
        }
    }
}


code inside the model:
Code:
public function save_Client_History($val1, $val2, $val3)
{
$data = array(
     'ip'=>$_SERVER['REMOTE_ADDR'],
     'rtt'=> $val1,
     'bt'=>$val2,
     'ltcy'=> $val3
     );
      
return $this->db->insert('clienthistory',$data);
          
}
#2

[eluser]NotDior[/eluser]
Welcome to CI!

Here's what I personally do on my models for inserts. It's a bit longer but I know if I'm getting a good insert or not.

Code:
public function save_Client_History($val1, $val2, $val3)
{
$data = array(
     'ip'=>$_SERVER['REMOTE_ADDR'],
     'rtt'=> $val1,
     'bt'=>$val2,
     'ltcy'=> $val3
     );
  
     $xResult = $this->db->insert('clienthistory',$data);


     if ($xResult) {
       return (TRUE);
     } else {
       return (FALSE);
     }          
}

Good luck.
#3

[eluser]neilmcgann[/eluser]
FALSE comes back if it failed, TRUE if successful.

The CI manual is a not so good at explaining some of the return vals - digging into DB_ driver and the query_builder source In system/database is always worth checking when the manual is vague.




Theme © iAndrew 2016 - Forum software by © MyBB