加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 在HTML表单下拉菜单中从DB连接的两个字符串之间添加空格

发布时间:2020-12-13 13:25:04 所属栏目:PHP教程 来源:网络整理
导读:我从同一个表中的2个DB列中提取数据,以 HTML格式填充下拉菜单.虽然我确实获得了两个数据,但我似乎无法用空格分隔它们.下面的代码是我这样做的一般尝试,但我尝试更改和省略空格的引号以及用单引号和双引号括起firstname和lastname.但似乎没有任何效果.到目前
我从同一个表中的2个DB列中提取数据,以 HTML格式填充下拉菜单.虽然我确实获得了两个数据,但我似乎无法用空格分隔它们.下面的代码是我这样做的一般尝试,但我尝试更改和省略空格的引号以及用单引号和双引号括起firstname和lastname.但似乎没有任何效果.到目前为止我离开了:Firstnamelastname.
<?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’]

我添加了别名作为名称,使生活更轻松

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读