CodeIgniter Forums
find() in model function returns 2d array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: find() in model function returns 2d array (/showthread.php?tid=77331)



find() in model function returns 2d array - mykeeeliling - 08-18-2020

Hi,

I'm just wondering if there is a way to make the find() function return a one dimensional array. I have a login function in my model and I want to run password_verify on it through

Code:
password_verify($password, $users['pwd']);


this is the code I'm familiar with in CI3 but every time I run this code. it gives an error of undefined index. so I tried to debug it with var_dump and print_r. this returns me the array result

array(1) {
  [0]=>
  array(12) {
    ["id"]=>
    string(1) "1"

....
   }
}

This is my model code that makes use of the return result of the find function.
Code:
public function checkLogin($userid, $password){
        $users = $this->where('emp_id', $userid)->find();
        if (!empty($users) && password_verify($password, $users[0]['pwd'])) {
            return true;
        } else {
            return false;
        }
    }

Thank you very much in advance! Good Day, Stay Safe, and Happy Coding.


RE: find() in model function returns 2d array - jreklund - 08-21-2020

Hi, are emp_id your primary key in said table? In that case you should use:

Code:
$this->find($userid);

As you don't specify an ID in there, you will get an array.

Or do it your way with first() instead.

Code:
$users = $this->where('emp_id', $userid)->first();

https://codeigniter.com/user_guide/models/model.html


RE: find() in model function returns 2d array - mykeeeliling - 08-22-2020

I have not declared 'emp_id' as my primary key. also, in my DB 'emp_id' is not my primary key but a unique foreign key. I'll try both of your suggestions and get back on this. Thank you!