Jump to content

Recommended Posts

Posted

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.

Posted

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.

Posted

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.

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...