/    Sign up×
Community /Pin to ProfileBookmark

How to check if a object is set

Put simple, I’m populating a user object based on a username entered

`$thisUser = $User->getByUsernameOrEmail($_POST[‘username’]);`

this returns a user object. if nothing is found by the username all my properties are still empty.

how can i check if one has been found?

user Class

private int $userId;
private string $userFirstName;
private string $userLastName;
private string $gender;
private string $dob;
private string $email;
private int $userLevel;
private $dbh;

public function __construct(database $database){
$this->dbh = $database;
}

public function getByUsernameOrEmail(string $username){

$sql = “SELECT * FROM `users` WHERE `userName` = ? OR `email` = ? LIMIT 1”;

$stmt = $this->dbh->prepare($sql);
$stmt->bind_param(‘ss’, $username, $username);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();

if ($result->num_rows > 0){
$this->userId = $row[‘userId’];
$this->userName = $row[‘userName’];
$this->userFirstName = $row[‘firstName’];
$this->userLastName = $row[‘lastName’];
$this->gender = $row[‘gender’];
$this->dob = $row[‘dob’];
$this->email = $row[’email’];
$this->userLevel = $row[‘accessLevel’];
}

return $this;
}

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmMay 07.2022 — Doesn't the isset function of PHP detect if an Object is there?
Copy linkTweet thisAlerts:
@NogDogMay 07.2022 — I'd just change that method to return false if no match is found, assuming that makes sense with what you are trying to do?
[code=php]
if ($result->num_rows > 0){
$this->userId = $row['userId'];
$this->userName = $row['userName'];
$this->userFirstName = $row['firstName'];
$this->userLastName = $row['lastName'];
$this->gender = $row['gender'];
$this->dob = $row['dob'];
$this->email = $row['email'];
$this->userLevel = $row['accessLevel'];
return $this; // we found something
}
return false; // nothing was found
}
[/code]
Copy linkTweet thisAlerts:
@kiwisauthorMay 07.2022 — @ginerjm#1643875

No because it is set. It just not populated.
Copy linkTweet thisAlerts:
@kiwisauthorMay 07.2022 — @NogDog#1643878

Yeah that's it. works perfectly.
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...