gpt4 book ai didi

php - 难以在 HTML 表单下拉菜单中从 DB 联系的两个字符串之间添加空格

转载 作者:可可西里 更新时间:2023-11-01 12:38:36 24 4
gpt4 key购买 nike

我正在从同一个表中的 2 个数据库列中提取数据,以填充 HTML 表单中的下拉菜单。虽然我确实得到了两条数据,但我似乎无法用空格将它们连接起来。下面的代码是我这样做的一般尝试,但我尝试更改和省略空格的引号以及将名字和姓氏括在单引号和双引号中。但似乎没有任何效果。到目前为止,我只剩下:姓氏。

<?php
//db connection
mysql_connect('localhost', 'root', "");
mysql_select_db('recy');

$sql = "SELECT CONCAT(firstname,' ',lastname) FROM technicians";
$result = mysql_query($sql);

echo "<select name='firstname',' ','lastname'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value'" . $row['CONCAT(firstname,' ',lastname)'] . "'>" . ucwords($row['CONCAT(firstname,' ',lastname)']) . "</option>";
}
echo "</select>";
?>

任何建议或建议表示赞赏。

最佳答案

CONCAT_WS , 是包含分隔符的 mySQL 函数,所以 ..

$sql = "SELECT CONCAT_WS(' ',firstname,lastname) as name FROM technicians";

然后使用 $row['name']

我添加了别名 as name 让生活更轻松

CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL.

关于php - 难以在 HTML 表单下拉菜单中从 DB 联系的两个字符串之间添加空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41658045/

24 4 0