phptek Posted November 26, 2011 Report Share Posted November 26, 2011 Hi Can you let me know how to assign values from the query below: public function getUser($username, $password) { $row = $this->fetchAll("username = '$username' AND password = '$password' AND role ='admin'"); return $row->toArray(); } getUser function will return the data in an array. How do I set each value from this array into each variable. $uname = username value from array $role = role value from array Thanks a lot. Quote Link to comment Share on other sites More sharing options...
jstern Posted November 29, 2011 Report Share Posted November 29, 2011 do you need to set this rowset to an array? I used to do this a lot when I was learning, just because i knew arrays better than objects, but if you dont need to, I would remove the ->toArray() when your returning. When you use fetchAll() your setting yourself up to retrieve more than 1 row if the condition are met. If more than one is returned this example could produce unexpected results; As an Array(): (not sure what you've assigned getUser() to, but lets say its $rows) $rows = $this>getUser($username, $password); $uname = $rows['username']; //replace 'username' with the name of your database column. $role = $rows['role']; //replace 'role' with whatever the column name is to get the value. //your array contains the databse column names as the array keys and the values as your array values. Play with var_dump($rows); and refresh to see everything contained. As an object / rowset (omitting the toArray() $rows = $this>getUser($username, $password); $uname = $rows->username;//replace 'username' with the name of your database column. $role = $rows->role; //replace 'role' with whatever the column name is to get the value. //leaving as a rowset you keep yourself open to doing other functions to your results, whereas as an array, you are more limited. Like I said, if your $rows has more than one result brought from the SQL query this wont work, since each result will be in its own array (multi-dimensional array). use fetchRow() instead of fetchAll() when selecting if you only want / expect one result. Quote Link to comment Share on other sites More sharing options...
phptek Posted November 30, 2011 Author Report Share Posted November 30, 2011 do you need to set this rowset to an array? I used to do this a lot when I was learning, just because i knew arrays better than objects, but if you dont need to, I would remove the ->toArray() when your returning. When you use fetchAll() your setting yourself up to retrieve more than 1 row if the condition are met. If more than one is returned this example could produce unexpected results; As an Array(): (not sure what you've assigned getUser() to, but lets say its $rows) $rows = $this>getUser($username, $password); $uname = $rows['username']; //replace 'username' with the name of your database column. $role = $rows['role']; //replace 'role' with whatever the column name is to get the value. //your array contains the databse column names as the array keys and the values as your array values. Play with var_dump($rows); and refresh to see everything contained. As an object / rowset (omitting the toArray() $rows = $this>getUser($username, $password); $uname = $rows->username;//replace 'username' with the name of your database column. $role = $rows->role; //replace 'role' with whatever the column name is to get the value. //leaving as a rowset you keep yourself open to doing other functions to your results, whereas as an array, you are more limited. Like I said, if your $rows has more than one result brought from the SQL query this wont work, since each result will be in its own array (multi-dimensional array). use fetchRow() instead of fetchAll() when selecting if you only want / expect one result. Thanks a lot. Will test it out. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.