I'm trying to pull out information from a database with PHP and have the following query:
$q = "SELECT CONCAT_WS(' ',FirstName,MiddleName,LastName) AS Name,
(NewCustomerID) AS customerid,
substring('Firstname',1,1, 'MiddleName'),
left(MiddleName,1) AS MN,
(LastName) AS LN
FROM customer
Where FN.MN.LN = 'username'";
$r = mysqli_query ($dbc, $q);
$num = mysqli_num_rows($r);
when I run this, I get info back but not the username, which I need:
$q = "SELECT CONCAT_WS(' ',FirstName,MiddleName,LastName) AS Name,
(NewCustomerID) AS customerid,
left('Firstname',1) as FN,
left(MiddleName,1) AS MN,
(LastName) AS LN
FROM customer".
Just stuck, have tried quite a few things and just can't get it to work.
$q = "SELECT CONCAT(left(FirstName,1),left(MiddleName,1),LastName) AS UserName, CONCAT_WS(' ',LastName,FirstName) AS Name, (NewCustomerID) AS customerid FROM customer ";
Since FN
, MN
, and LN
are aliases, you need to use HAVING
instead of WHERE
.
HAVING FN.MN.LN = 'username'
Though, FN.MN.LN
is a syntax error, I assume you meant CONCAT(FN,MN,LN)
?
HAVING CONCAT(FN,MN,LN) = 'username'
You are not quoting the query correctly, Firstname is a column name:
SELECT
CONCAT_WS(' ',FirstName,MiddleName,LastName) AS Name,
(NewCustomerID) AS customerid,
LEFT(Firstname,1) as FN,
LEFT(MiddleName,1) AS MN,
(LastName) AS LN
FROM customer
WHERE CONCAT(FN,MN,LN) = 'username'