I’m makeing a search filed for a table that has 25+ columns in it. Two of them are a First Name and a Last name. I can get the CONCAT to search them fine alone but when I add the other OR selections I get errors. Can anyone point me in the right direction? Any help would be great.
More details:
I’m tring to figure this out here.. I have two different SELECT statments that are working fine #1 is
$query = "SELECT `CustomerID`, `CompanyName`, `ContactFirstName`, `ContactLastName`,
`BillingAddress`, `BillingAddress2`, `City`, `PostalCode`, `PhoneNumber`, `FaxNumber`, `EMail`
FROM `Customers` WHERE `CustomerID` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `CompanyName` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `ContactFirstName` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `ContactLastName` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `BillingAddress` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `BillingAddress2` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `PhoneNumber` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `EMail` LIKE '%".mysql_real_escape_string($search_text)."%'";
And #2 is
SELECT *, CONCAT_WS(' ',ContactFirstName,ContactLastName) AS FullName FROM `Customers` HAVING
FullName LIKE '%".mysql_real_escape_string($search_text)."%'";
I’ve been unable to figure out how to add the second select into the first one.
—EDIT—
After a1ex07’s post I redid the query and it’s working they way I was looking for…
$query = "SELECT `CustomerID`, `CompanyName`, `ContactFirstName`, `ContactLastName`,
BillingAddress`, `BillingAddress2`, `City`, `PostalCode`, `PhoneNumber`, `FaxNumber`, `EMail`
FROM `Customers`
WHERE `CustomerID` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `CompanyName` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `BillingAddress` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `BillingAddress2` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `City` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `PostalCode` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `PhoneNumber` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `FaxNumber` LIKE '%".mysql_real_escape_string($search_text)."%'
OR `EMail` LIKE '%".mysql_real_escape_string($search_text)."%'
OR CONCAT_WS(' ',ContactFirstName,ContactLastName) LIKE
'%".mysql_real_escape_string($search_text)."%'";
$query_run = mysql_query($query);
while ($query_row = mysql_fetch_assoc($query_run)) {
$CustomerID = $query_row['CustomerID'];
$CompanyName = $query_row['CompanyName'];
$ContactFirstName = $query_row['ContactFirstName'];
$ContactLastName = $query_row['ContactLastName'];
$BillingAddress = $query_row['BillingAddress'];
$BillingAddress2 = $query_row['BillingAddress2'];
$City = $query_row['City'];
$PostalCode = $query_row['PostalCode'];
$PhoneNumber = $query_row['PhoneNumber'];
$FaxNumber = $query_row['FaxNumber'];
$EMail = $query_row['EMail'];
echo $CustomerID.' '.$CompanyName.' '.$ContactFirstName.' '.$ContactLastName.'
'.$BillingAddress.' '.$BillingAddress2.' '.$City.' '.$PostalCode.' '.$PhoneNumber.'
'.$FaxNumber.' '.$EMail.'<br>';
It seems to be doing what I want, unless there would be a way to get the FullName variable back in?
If I understood you well, you are trying to add
WHERE ... OR FullName LIKE ...to the first query. It fails because you cannot use field aliases inWHERE. You need to doWHERE ... OR CONCAT_WS(' ',ContactFirstName,ContactLastName) LIKE ....Updated:
Just add it to
SELECT:SELECT ..., CONCAT_WS(' ',ContactFirstName,ContactLastName) AS FullName.Once again, the problem is that aliases cannot be used in
WHERE.There are some ways to deal with it:
WHERE:SELECT ..., CONCAT_WS(' ',ContactFirstName,ContactLastName) AS FullNameWHERE ... OR CONCAT_WS(' ',ContactFirstName,ContactLastName)
HAVING:SELECT ..., CONCAT_WS(' ',ContactFirstName,ContactLastName) AS FullNameWHERE ...
HAVING FullName ...
SELECT * FROM (SELECT ..., CONCAT_WS(' ',ContactFirstName,ContactLastName) AS FullName
...)a
WHERE ... OR FullName ....
Your final query should look like: