1

Hi I have the following code;

if( ! empty( $post['search-bar'] ) ) {
    $search_data = preg_replace("#\s\s#is", '', preg_replace("#[^\w\d\s+]#is", '', $post['search-bar'] ) );
    $data_array = explode( " ", $search_data );
    $data_array = "'%" . implode( "%' OR '%", $data_array ) . "%'"; 
    $query =  "
SELECT CONCAT( PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME) AS 'STRING'
FROM `" . ACCOUNT_TABLE . "` 
WHERE STRING LIKE ( " . $data_array . " ) 
  AND BUSINESS_POST_CODE LIKE '" . substr(P_BUSINESS_POST_CODE, 0, 4) . "%'";

    $q = mysql_query( $query, $CON ) or die( "_error_" . mysql_error() );   
    if( mysql_num_rows( $q ) != 0 ) {
        die();
    }
}

Problem is I want to use the temp col 'STRING' in the where clause but is returning 'unknown coloumn STRING Can any one point me in the right direction, regards Phil

I have printed out the query;

SELECT PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME 
FROM nnn_accounts 
WHERE 
  CONCAT( PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME) 
  LIKE ( '%web%' OR '%design%' ) 
AND BUSINESS_POST_CODE LIKE 'NG19%'

4 Answers 4

1

Wrap STRING in ` instead of '

Should be: ..AS `STRING`

And then anywhere you refer to STRING should be `STRING`

Edit: Here this should solve it:

SELECT CONCAT( PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME) AS `STRING`
FROM `" . ACCOUNT_TABLE . "` 
WHERE BUSINESS_POST_CODE LIKE '" . substr(P_BUSINESS_POST_CODE, 0, 4) . "%'"
HAVING `STRING` LIKE ( " . $data_array . " ) ;
Sign up to request clarification or add additional context in comments.

3 Comments

still getting Unknown column 'STRING' in 'where clause'
Try using HAVING `STRING` LIKE ( " . $data_array . " ) (Do this after the WHERE clause
thanks for the input. Im going to have to mark yours as correct due to my question, but does the same as Col. Shrapnel instructed. My problem being i am not getting correct results. how come web or design will return results but web design will not??
1

Yes, it looks like you just can't use the temp col in the where clause just specify it as is:

WHERE CONCAT( PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME) LIKE '%$data_array%') 

3 Comments

I get no errors but getting no results. I have printed out the query; SELECT PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME FROM nnn_accounts WHERE CONCAT( PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME) LIKE ( '%web%' OR '%design%' ) AND BUSINESS_POST_CODE LIKE 'NG19%'
if i enter web i get results. if i type in design i get results but web design gets no results.
your use of LIKE clause is just ridiculous
0

Remove the quotes around 'STRING'

Comments

0

The syntax that was more suitable was

SELECT CONCAT( PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME) AS `STRING` FROM `tablename` WHERE BUSINESS_POST_CODE LIKE 'NG19%' HAVING `STRING` LIKE '%web%' OR `STRING` LIKE '%design%'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.