开发者

MYSQL/PHP/AJAX Search using SELECT, CONCAT, OR and LIKE on multiple columns

开发者 https://www.devze.com 2023-04-08 08:25 出处:网络
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 开发者_StackOverflowalone but when I add the

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 开发者_StackOverflowalone 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 in WHERE. You need to do WHERE ... OR CONCAT_WS(' ',ContactFirstName,ContactLastName) LIKE ....

Updated:

It seems to be doing what I want, unless there would be a way to get the FullName variable back in?

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:

  1. Use expression in WHERE : SELECT ..., CONCAT_WS(' ',ContactFirstName,ContactLastName) AS FullName WHERE ... OR CONCAT_WS(' ',ContactFirstName,ContactLastName)
  2. Use alias in HAVING : SELECT ..., CONCAT_WS(' ',ContactFirstName,ContactLastName) AS FullName WHERE ... HAVING FullName ...
  3. Use derived tables :
    SELECT * FROM ( SELECT ..., CONCAT_WS(' ',ContactFirstName,ContactLastName) AS FullName
    ...)a
    WHERE ... OR FullName ....

Your final query should look like:

    $query = "SELECT `CustomerID`, `CompanyName`, `ContactFirstName`, `ContactLastName`, 
BillingAddress`, `BillingAddress2`, `City`,  `PostalCode`, 
 `PhoneNumber`, `FaxNumber`, `EMail`,  
 CONCAT_WS(' ',ContactFirstName,ContactLastName) AS FullName
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)."%'";


On #1 What if you put a space after the single quote? ' "; In other words so your sql doesn't run together, proper spacing.

Also, echo your SQL and try look at it to see.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号