craigglenn
Technical User
This seemed like a simple issue but has proven difficult for me.
I have a directory on a site I manage. Wordpress3/MySQL 5.0.7/PHP 5.1.6
I have 1K plus company_name's with a '-' in the name.
My problem is if I include the '-' I get results ie adams-green returns the company I was looking for but if I search 'adams green' it will not.
Here is the code I use with my attempt to resolve with a simple OR statement. It works fine for me in straight sql but not in my PHP code. I am sure it is a syntax issue but can't track it down. I have include the code below. I do not get an error or NO RESULTS message. the page just hangs as if I put it in a loop or have a syntax problem. I have to reload the page to clear it up. If I remove the OR logic it works fine but with the '-' problem.
PHP
$query = "SELECT * FROM ".BIZDIRDBTABLE." l LEFT JOIN ".BIZDIRCATTABLE." c ON (c.category_id = l.category_id) WHERE status='1' ";
if(!empty($searchTerms))
{
$query .= $wpdb->prepare("AND (company_name LIKE %s OR REPLACE(company_name,'-',' ') LIKE %s) ORDER BY c.category_id ASC, company_name ASC",'%'.$searchTerms.'%');
}
SQL that works for my testing purposes.
SET @v = 'adams green';
SELECT *
FROM `wp_biz_listings`
WHERE status=1
AND (replace(company_name,'-',' ') like concat('%',@v,'%')
OR company_name like concat('%',@v,'%'))
Thanks in advance for your help.
"You can have anything in life you want if you help enough people get what they want"
Zig Ziggler
I have a directory on a site I manage. Wordpress3/MySQL 5.0.7/PHP 5.1.6
I have 1K plus company_name's with a '-' in the name.
My problem is if I include the '-' I get results ie adams-green returns the company I was looking for but if I search 'adams green' it will not.
Here is the code I use with my attempt to resolve with a simple OR statement. It works fine for me in straight sql but not in my PHP code. I am sure it is a syntax issue but can't track it down. I have include the code below. I do not get an error or NO RESULTS message. the page just hangs as if I put it in a loop or have a syntax problem. I have to reload the page to clear it up. If I remove the OR logic it works fine but with the '-' problem.
PHP
$query = "SELECT * FROM ".BIZDIRDBTABLE." l LEFT JOIN ".BIZDIRCATTABLE." c ON (c.category_id = l.category_id) WHERE status='1' ";
if(!empty($searchTerms))
{
$query .= $wpdb->prepare("AND (company_name LIKE %s OR REPLACE(company_name,'-',' ') LIKE %s) ORDER BY c.category_id ASC, company_name ASC",'%'.$searchTerms.'%');
}
SQL that works for my testing purposes.
SET @v = 'adams green';
SELECT *
FROM `wp_biz_listings`
WHERE status=1
AND (replace(company_name,'-',' ') like concat('%',@v,'%')
OR company_name like concat('%',@v,'%'))
Thanks in advance for your help.
"You can have anything in life you want if you help enough people get what they want"
Zig Ziggler