Skip to content
Advertisement

How to join two fields in one array?

I have the following code:

$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"
    );

I need the $name to have both the first name and last name. How would I do this?

How would I declare the $name =

Advertisement

Answer

You can use CONCAT function:

SELECT ...
       CONCAT(first_name, " ", last_name) AS name
       ...

In your code:

$result = mysql_query("SELECT CONCAT(first_name, " ", last_name) AS name, employeeID FROM ecc_employee WHERE first_name != '' ");

And call it with:

$row['name']

You can also name employeeID as id to skip reassign array keys in while loop:

$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;
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement