Page 1 of 1

removing hyphen from search string

Posted: Fri Sep 21, 2007 5:26 am
by mikeeeeeeey
Hi guys,

I've been having some major problems with a script I'm developing which basically queries the database using a SELECT statement. The trouble is some of the entries contain hyphen's and this is letting strings past which should have been found in the database.

I've tried replacing the character beforehand using str_replace() and used addslashes() and mysql_real_escape_string() but to no avail. I've just read up a bit on Regex and I was just wondering is there any way I can use this to achieve the result I'm after.


Here's the SQL below (you'll appreciate why I'm tearing my hair out):

Code: Select all

$sql   = "SELECT * FROM categories_description WHERE categories_name = '" . $section . "'";
Can anyone help at all?

Thanks for reading.

Posted: Fri Sep 21, 2007 6:20 am
by VladSun
You may find REPLACE(str,from_str,to_str) useful.

Posted: Fri Sep 21, 2007 6:35 am
by mikeeeeeeey
Thanks, still the same output though.
Am I right in thinking there is a MySQL bug concerning hyphens and its effect on a SELECT statement?

Posted: Fri Sep 21, 2007 6:51 am
by VladSun
As far as I can understand you want this to be found:

$section = "bla bla";

categories_name entiries:
bla-bla
bla bla
bla-\nbla

is it right?

Posted: Fri Sep 21, 2007 7:51 am
by mikeeeeeeey
Say...
$section[0] = "blah - blah";
$section[1] = "blah - blah";

Everytime I examine a $section I want to query this to categories_name and, if there are no other entries, insert that into categories_name, but so far the example above would insert both $section strings (since they contain a hyphen).

Posted: Fri Sep 21, 2007 8:49 am
by VladSun
I've tested your example and it worked ...
Try echoing the $sql variable and see what it contains.

Posted: Fri Sep 21, 2007 10:11 am
by mikeeeeeeey
SELECT `categories_name`
FROM `categories_description`
WHERE `categories_name` = CONVERT( _utf8 'Visual - Conference & Presentation'

it should say 'Audio Visual - Confere...'

This is something I've put together from PHPmyadmin. Why is it removing the first word?