我有以下代码:
$result = mysql_query("SELECT first_name, last_name, employeeID FROM ecc_employee WHERE first_name != '' ");
$resources = array();
while ($row=mysql_fetch_array($result)){
$name = ($row['first_name']);
$id = $row['employeeID'];
$resources[] = array(
'name' => "$name",
'id' => "$id"
);
我需要$ name同时拥有名字和姓氏。我该怎么做?
我如何声明$ name =
答案 0 :(得分:2)
$name = $row['first_name'].' '.$row['last_name'];
或者你可以按照建议使用mysql CONCAT函数。 更多信息:http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
答案 1 :(得分:2)
您可以使用CONCAT
功能:
SELECT ...
CONCAT(first_name, " ", last_name) AS name
...
在您的代码中:
$result = mysql_query("SELECT CONCAT(first_name, " ", last_name) AS name, employeeID FROM ecc_employee WHERE first_name != '' ");
并将其命名为:
$row['name']
您还可以将employeeID
命名为id
以跳过在while循环中重新分配数组键:
$result = mysql_query("SELECT CONCAT(first_name, " ", last_name) AS name, employeeID AS id FROM ecc_employee WHERE first_name != '' ");
$resources = array();
while ($row = mysql_fetch_array($result)){
$resources[] = $row;
}
答案 2 :(得分:0)
使用PDO
// of course connect goes first
$sql = "SELECT SELECT CONCAT(first_name, ' ', last_name) name, employeeID id
FROM ecc_employee WHERE first_name != ''";
$stm = $pdo->prepare($sql);
$stm->execute();
$resources = $stm->fetchAll();