DEV Community

Discussion on: Why is affectedRows method returning 0 in CodeIgniter?

Collapse
 
majeo profile image
majeo

got here when surfing for some issue , read yer solution , simple good clean .. TQ for sharing ..

i'm some how not fimilar yet with CI , I couldn't get this peace of code or let's say didn't know why need it ..
if($result->num_rows() == 1){

return $result->row(0)->id; //this one why row(0) ??!!

Collapse
 
dmahely profile image
Doaa Mahely

Hey, glad I could help.
Can you post a full snippet of code?

Collapse
 
majeo profile image
majeo

thnx for you ..
here you re a simple user_model (Models file = typically deal with DB as I learnt) ..
it is just typical method of comparing what user enters with what data we have in db ..
you won't need to see the (View file) I guess .. :)
it's only one row comes back from db, let's say one result comes back , why need to involve that Zero :
row(0)->id; ?!!
to my simple knowledge there is no need for Zero in that bracket .. right ?!

Thread Thread
 
majeo profile image
majeo

posting screenshot image , has not been uploaded for some reason but here you re : the code :

<?php
class User_model extends CI_Model{
public function register(){
//data array to get all data from the form
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'username' => $this->input->post('user_name'),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('users',$data);
return $insert;
/if this all above go then it will retrun true if not then it will return false, that's why we
testing it there on user.php by if() statement
/
}

    //login method: is logic funtion that it's able to work there on users.php if($user_id)
    public function login($user_name, $password){
        //validate with the table
        $this->db->where('username',$user_name);
        $this->db->where('password',$password);
        //restoring the row of the matching in a var
        $result = $this->db->get('users');

        if($result->num_rows() == 1){    //if there is an actual result 
            return $result->row(0)->id;   //basiclly return the only id from the row , but why the zero !!!
        }else{    // if not then do next 
            return false; 
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

?>