Home » Php » php – difficulty adding whitepace between two strings concatted from DB in HTML form drop down menu

php – difficulty adding whitepace between two strings concatted from DB in HTML form drop down menu

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am pulling data from 2 DB columns in same table to populate drop down menu in HTML form. Although I do get both pieces of data, I cannot seem to concat them with white space separate them. The code below is my general attempt to do so, but I have tried changing and omitting quotes for the whitespace as well as enclosing firstname and lastname in single and double quotes. But nothing seems to work. I am left thus far with: 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>";
        ?>

Any advice or suggs appreciated.

How to&Answers:

CONCAT_WS, is the mySQL function to include a separator so ..

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

then use $row['name']

i added the alias as name to make life easier

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.