Welcome Guest, Not a member yet? Register   Sign In
find() in model function returns 2d array
#1

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.
Reply
#2

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
Reply
#3

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!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB