Santosh Khatri
Santosh Khatri

Reputation: 487

select query not executing in codeigniter?

I am trying to execute select query in codeigniter and i create function for the same like below:

public function validate(){
    $username = $this->security->xss_clean($this->input->post('username'));
    $password = $this->security->xss_clean($this->input->post('password1'));
    $type = $this->security->xss_clean($this->input->post('utype'));
    $password=md5($password);

    $this->db->select('*');
    $this->db->from('ci_users');
    $this->db->where('email', $username);
    $this->db->where('password', $password);
    $this->db->where('utype', $type);

    // Run the query
    $query = $this->db->get();

    // Let's check if there are any results
    if($query->num_rows == 1)
    {
        // If there is a user, then create session data
        $row = $query->row();
        $data = array(
                'userid' => $row->id,
                'fname' => $row->fname,
                'lname' => $row->lname,             
                'email' => $row->email,
                'utype' =>$row->utype,
                'phone' =>$row->mobile,
                'city' =>$row->city,
                'state' =>$row->state,
                'country' =>$row->country,
                'validated' => true
                );
        $this->session->set_userdata($data);
       echo '<pre>';print_r($data); exit();
        //return true;
    }
    else
    {
        echo "OPPS some error";
        //return false;
    }
}

it is always showing opps some error, even i am enter correct username and password. why select query not run? i have did wrong something. please help me.

Upvotes: 1

Views: 1333

Answers (2)

Razib Al Mamun
Razib Al Mamun

Reputation: 2713

You are used :

if($query->num_rows == 1)

Correction is like bellow :

if($query->num_rows() == 1)

Upvotes: 1

Antony
Antony

Reputation: 4330

Here are a few things to help:

1) You don't need the db->select('*') (just a handy tip)

2) Replace $this->db->get() with $this->db->count_all_results(). Then if ($query == 1) will definitely count (I'm afraid I don't recognise $query->num_rows)

3) In your model or above your select - put this line in: $this->db->conn_id->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); This will break your code by generating an exception if there is a problem with your SQL query.

From there you should be able to diagnose your problem.

Upvotes: 0

Related Questions