Concat将两个字段放入下拉菜单中

时间:2013-06-24 09:53:25

标签: php sql concat

我试图简单地从表中显示两个字段,例如firstname,lastname,组合它们并显示在可以选择的下拉菜单中,并与用户输入的其他数据一起存储。下面,适用于一个领域,但我正在努力结合姓,我尝试连续但我认为我做错了。提前谢谢。

//Drop Down Select
  $sql = "SELECT concat (firstname, lastname) as username  FROM users_tbl";
$result = pg_query($sql);
echo "<select name='firstname'>";

while ($row = pg_fetch_array($result)) {
 echo "<option value='" . $row['firstname'] . "'>" . $row['firstname'] ."</option>";

}
echo "</select>";
// close connection

2 个答案:

答案 0 :(得分:1)

要么依赖SQL ......

while ($row = pg_fetch_array($result)) {
    echo "<option value='" . $row['username'] . "'</option>";
}

或者不在SQL中使用CONCAT并使用PHP执行:

while ($row = pg_fetch_array($result)) {
    echo "<option value='" . $row['firstname'] . " " . $row['lastname'] ."'</option>";
}

但不要两者都做。

答案 1 :(得分:1)

尝试这种方式: -

$sql = "SELECT concat (firstname, lastname) as username  FROM users_tbl";
$result = pg_query($sql);
echo "<select name='firstname'>";

while ($row = pg_fetch_array($result)) {
echo '<option value="'.$row['username'].'" >'.$row['username'].'</option>';
}
echo "</select>";
// close connection