James
James

Reputation: 567

PHP Using Object to Retrieve DB data

I am wondering if someone could help me. I am new to PHP OOP and would like some guidance with using objects.

I am making a login script and the functions I mention below are all from the class file.

Class USER{

public function userLogin($username,$password)
{   
    $statusY = "Y";

    $stmt  = $this->connection->prepare("SELECT * FROM tbl_users WHERE user_name=:userName  LIMIT 1");
    $stmt ->execute(array(":userName"=>$username));
    $row = $stmt ->fetch(PDO::FETCH_ASSOC);

        if($stmt->rowCount() == 1)
        {

        $this->_user = $row; // Assign user details

        $_SESSION['userSession'] = $row['user_id'];

        }
}


public function getUser()
{
    return $this->_user;
}

Ok so I have the getUser() function and then assign $this->_user = $row so I can retrieve the user info from the database. Now I want to acheive a couple of things from this but not sure how to go about it.

How would I go about calling $row['user_id'] in another function within the same class?

So basically

public function test()
{
$user_id = $this->_user(user_id);
$username = $this->_user(username);
}

How would I do this correctly?

Also if I want to call the information in a page such as the User Homepage.

 $user_home = new USER();
 $userID = $user_home->getUser(user_id);

 echo $userID;

If anyone could give me some guidance as to how I can move forward with this I would greatly appreciate it. Thanks

Upvotes: 1

Views: 58

Answers (2)

SaschaM78
SaschaM78

Reputation: 4497

Let's start with a basic statement regarding method and class naming: I wouldn't repeat the class' topic over and over again (in the above case "user"), instead just remove it from the method name:

class User 
{
  private $info= array();
  private $authenticated= FALSE;

  public function login ($username, $password)
  {
    // do your stuff 
    ...
    // set in case that user name and password have been found
    $this->info= $row;
    $this->authenticated= TRUE;
  }

  public function isAuthenticated () 
  {
    return $this->authenticated;
  }

  /**
  * returns all info from a given user
  */
  public function get ()
  {
    return $this->info;
  }

  /**
  * returns a single field
  */
  public function getField ($fieldName) {
    if (isset($this->info[$fieldName]) {
      return $this->info[$fieldName];
    } else {
      return FALSE;
    }
  }
}

Use the getField(FIELD) method to return only a single element of the user's row and get() to return all values.

$user= new User();
$user->login ($username, $password);
if ($user->isAuthenticated()) {
  $home= $user->getField('home');
  print sprintf('%s\'s home is %s', $username, $home);
}

It's also advisable to create a class for the user database table (i.e. class UserModel) and another one handling user functions (i.e. class UserAuth) which uses the UserModel class. This makes exchanging the underlying authentication source more easy.

Upvotes: 1

Anish Chandran
Anish Chandran

Reputation: 447

Class USER
 {
   public $_user;

   public function userLogin($username,$password)
   {   
      $statusY = "Y";

    $stmt  = $this->connection->prepare("SELECT * FROM tbl_users WHERE user_name=:userName  LIMIT 1");
$stmt ->execute(array(":userName"=>$username));
$row = $stmt ->fetch(PDO::FETCH_ASSOC);

    if($stmt->rowCount() == 1)
    {

    $this->_user = $row; // Assign user details

    $_SESSION['userSession'] = $row['user_id'];

    }
 }


public function getUser()
 {
return $this->_user;
}
 }

Upvotes: 0

Related Questions