0

我有以下代码:

$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 =

4

3 回答 3

2
$name = $row['first_name'].' '.$row['last_name'];

或者,您可以按照建议使用 mysql CONCAT 函数。更多信息在这里:http ://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat

于 2013-04-08T14:42:24.150 回答
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']

您还可以命名employeeIDid跳过在 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;
}
于 2013-04-08T14:42:28.677 回答
0

使用PDO

// of course connect goes first
$sql = "SELECT CONCAT(first_name, ' ', last_name) AS name, employeeID AS id 
        FROM ecc_employee WHERE first_name != ''";
$stm = $pdo->prepare($sql);
$stm->execute();
$resources = $stm->fetchAll();
于 2013-04-08T14:48:00.227 回答