【发布时间】:2019-12-15 13:55:52
【问题描述】:
我有以下 sql 可以为特定用户提取信息:
public function getForUniqueID($unique_id) {
$sql = "SELECT `first_name` as first_name, `last_name` as last_name FROM `{$this->table}` WHERE `unique_id` = ? LIMIT 1";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([$unique_id]);
if ($stmt->rowCount() == 0) return null;
$tournament = $stmt->fetchAll(PDO::FETCH_NAMED);
$tournament = $tournament[0];
$tournament = $this->applyRelations($tournament);
return $tournament;
}
这会返回一个数组对象,但我想将 first_name 和 last_name 值作为一个字符串返回,格式如下:first_name " " last_name
如何修改上述方法以将名字和姓氏作为单个字符串返回?
【问题讨论】:
-
implode数组。 -
另外,如果您的查询将只返回一行,您可以只使用
fetch而不是fetchAll。那你就不用$tournament = $tournament[0]了。 -
您需要在该 CONCAT 中使用单引号。您的 SQL 字符串已经用双引号括起来了。
-
噢!谢谢@Don'tPanic!修复它
-
不客气!不过,我认为最好将您的问题的更改还原。就像现在一样,其他任何出现的人都很难看到答案是如何真正回答问题的,因为他们现在都有相同的代码。