I have the following function that I created for pulling a user's preferred name:
public function getPreferredName($unique_id) {
$sql = "SELECT CONCAT(first_name, ' ', last_name) as name FROM `{$this->table}` WHERE `unique_id` = ? LIMIT 1";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([$unique_id]);
if ($stmt->rowCount() == 0) return null;
$preferredName = $stmt->fetchColumn();
return $preferredName;
}
This table is a "users" table made up of individuals and companies.
There are some additional fields that I would like to consider in this query:
- "display_as" (individual or company)
- "company_name" (used when display_as = company)
Now i'd like to modify the method above to return either the first and last name OR the company name based on whether the value of the "display_as" column equals "individual" or "company".
If "display_as" equals "individual" return the first_name and last_name fields.
If "display_as" equals "company" return the company_name field.
How can I modify the method above to return either the first_name and last_name together or the company_name value based on the "display_as" column?
Thanks for the help. Cheers
display_as
and the other fields and check the data returned – user10051234fetchColumn
now, you don't need theas name
alias.fetchColumn
will just grab the first column regardless of name. And ifunique_id
is really unique,LIMIT 1
is redundant. (Neither of those are really problems, just extra code you don't need.) – Don't Panic