简体   繁体   中英

Codeigniter return() returns only one field in db

I am having this problem where my code is returning only one value.

class Userlogin extends CI_Model {

    function login($username,$password) {
       $this -> db -> select('userid', 'username', 'password');
       $this -> db -> from('user');
       $this -> db -> where('username', $username);
       $this -> db -> where('password', $password);
       $this -> db -> limit(1);

       $query = $this -> db -> get();
         if($query -> num_rows() == 1)
           {             
             return $query->result();            
           }
           else
           {
             return false;
           }
    }

}

Here is the code that is using the particular model

 if($result)
{     
     $sess_array = array();

     foreach($result as $row)
     {     
       $sess_array = array(
         'userid' => $row->userid,
         'username' => $row->username
       );       
       $this->session->set_userdata('logged_in', $sess_array);
     }

The problem is that it is returning only the userid but not the respective username for that id.

In your code, you've got the following line:

$this -> db -> select('userid', 'username', 'password');

You cannot pass multiple arguments to the select method of the db layer of Codeigniter. It has to be comma-seperated, like this:

$this -> db -> select('userid, username, password');

Hope this helped you out!

Reference: http://ellislab.com/codeigniter/user-guide/database/active_record.html#select

select method expects a string containing all the columns you want. Try

$this -> db -> select('userid, username, password');

you can do something like this

Model :

class Userlogin extends CI_Model {

function login($username,$password) {
   $this -> db -> select('userid, username, password');
               -> from('user')
               -> where('username', $username)
               -> where('password', $password)
               -> limit(1);

   $query = $this -> db -> get();

     if($query -> num_rows() == 1)
       {             
         return $query->result_array();            
       }

      return false;
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM