I have code to propagate a drop down list that displays people's names. The problem is that the first name and last name are separate fields in the database, and I can only get the drop down list to display one of them. Here's my code:

$result = mysql_query("SELECT id, `first`, `last` FROM agents ORDER BY agents.id");

<form name="Events" method="post" action="<?php echo $php_SELF;?>">

<table width="500" border="1" align="center" cellpadding="4" cellspacing="0">
<td width="200" align="left">
<select name="Person" id="Person">
<option selected>Select</option>

<?php while ($row = mysql_fetch_array($result)){?>
<option><?php echo $row['last'];?></option>
<?php }?>

If I run this in my browser, I get a list of last names. How can I get both the first names and the last names?


8 Years
Discussion Span
Last Post by dewhickey

Depends on what you expect to happen next. You can act both in PHP, manipulating the returned array result, or you can change your SQL so that sql returns the first and last name as one column, respectively as one value.

I would go with mysql change of the select statement like this.

$result = mysql_query("SELECT id, concat(first, ' ',last) as fullname FROM agents ORDER BY agents.id");

This code will return the first and last names with space between them.
Then you can access the fullname as associative array, using e.g.

Try and advise if it works for you.
Also advise if you shall need to separate the names again e.g. during submit or something.


Thanks. That worked fine. Now, I just have to continue learning.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.