Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6634485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:56:07+00:00 2026-05-25T22:56:07+00:00

I’m makeing a search filed for a table that has 25+ columns in it.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:56:08+00:00Added an answer on May 25, 2026 at 10:56 pm

    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)."%'";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I need to write an AS3 program to search for a certain keyword in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.