Jump to content
Killersites Community
Sign in to follow this  
phptek

Retreiveing data from array

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×