Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-15-2006, 12:59 PM   PM User | #1
MRMAN
Regular Coder

 
Join Date: Jan 2006
Location: Preston, Lancashire, England
Posts: 285
Thanks: 0
Thanked 0 Times in 0 Posts
MRMAN is an unknown quantity at this point
Search: Remove quotes from database for search

Hello,.

I am making a book site and currently in the database are book titles likes "John's Trip" and "Peter's new bike".
I have a search function where the customer can type in "Peter's" and it will bring up everything with "Peter's" in it.

But i was wonderind is there any way i can search for "Peters" and "Johns".
MRMAN is offline   Reply With Quote
Old 07-15-2006, 01:34 PM   PM User | #2
GJay
Senior Coder

 
Join Date: Sep 2005
Posts: 1,791
Thanks: 5
Thanked 36 Times in 35 Posts
GJay is on a distinguished road
you can use the mysql replace() function:
http://dev.mysql.com/doc/refman/5.0/...functions.html
or do it with whichever language you're using, PHP would be:
PHP Code:
$query='SELECT * FROM books WHERE title=\''.str_replace('\'','','Peter\'s Book').'\''
GJay is offline   Reply With Quote
Old 07-15-2006, 01:47 PM   PM User | #3
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
Quote:
Originally Posted by MRMAN
Hello,.

I am making a book site and currently in the database are book titles likes "John's Trip" and "Peter's new bike".
I have a search function where the customer can type in "Peter's" and it will bring up everything with "Peter's" in it.

But i was wonderind is there any way i can search for "Peters" and "Johns".
kinda depends on what your current select looks like.
i imagine your select looks like
SELECT foobar FROM yourtable WHERE title LIKE '%Peter\'s%'

which is probably build like
PHP Code:
$sql="SELECT foobar FROM yourtable WHERE title LIKE '%"$_POST['searchterm'] ."%'" 
one option would be to replace the ' by % like
PHP Code:
$sql="SELECT foobar FROM yourtable WHERE title LIKE '%"str_replace("'"'%'$_POST['searchterm']) ."%'" 
but this would also match a title like 'Peter knows'

another, more stringent but less performant option would be
PHP Code:
$clause "title LIKE '%"$_POST['searchterm'] ."%'";
if (
strpos($_POST['searchterm'], "'") !== False){
    
$clause .= " OR title LIKE '%"str_replace("'"''$_POST['searchterm']) ."%'"
}
$sql="SELECT foobar FROM yourtable WHERE "$clause
and there is probebly also a regex possebility...
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 07-15-2006, 02:12 PM   PM User | #4
MRMAN
Regular Coder

 
Join Date: Jan 2006
Location: Preston, Lancashire, England
Posts: 285
Thanks: 0
Thanked 0 Times in 0 Posts
MRMAN is an unknown quantity at this point
Thanks chaps. But i don't think i was clear. Sorry.

Basically what i want is when someone types in "Peters" it will find "Peter's" and "Peters"

Thanks
MRMAN is offline   Reply With Quote
Old 07-15-2006, 05:32 PM   PM User | #5
vinyl-junkie
$object->toCD-R(LP);


 
vinyl-junkie's Avatar
 
Join Date: Jun 2003
Posts: 3,053
Thanks: 2
Thanked 22 Times in 22 Posts
vinyl-junkie is on a distinguished road
Quote:
Originally Posted by MRMAN
Thanks chaps. But i don't think i was clear. Sorry.

Basically what i want is when someone types in "Peters" it will find "Peter's" and "Peters"

Thanks
You typed in "Peters" twice here. Did you mean something else? That last example that raf gave you will find what you've stated here. Is there something else you're trying to do that we're not clear on?
__________________
Music Around The World - Collecting tips, trade
and want lists, album reviews, & more
SNAP to it!
vinyl-junkie is offline   Reply With Quote
Old 07-15-2006, 10:17 PM   PM User | #6
MRMAN
Regular Coder

 
Join Date: Jan 2006
Location: Preston, Lancashire, England
Posts: 285
Thanks: 0
Thanked 0 Times in 0 Posts
MRMAN is an unknown quantity at this point
ok sorry. in the database i have this

Code:
bookId      title                   author
1             Peter's Story       Peter
2             Anna's Dress       Anna
and when i search on the website i want to be able to type in "Peter" and "Peter's" to find the first record. And "Annas" and "Anna's" to find the second record.


Thank you for everyones help
MRMAN is offline   Reply With Quote
Old 07-15-2006, 11:58 PM   PM User | #7
GJay
Senior Coder

 
Join Date: Sep 2005
Posts: 1,791
Thanks: 5
Thanked 36 Times in 35 Posts
GJay is on a distinguished road
you'll need the mysql replace function then, did you even look at the page?
SELECT * FROM books WHERE replace(title,'\'','') LIKE '%Peters%';
GJay is offline   Reply With Quote
Old 07-17-2006, 01:33 PM   PM User | #8
MRMAN
Regular Coder

 
Join Date: Jan 2006
Location: Preston, Lancashire, England
Posts: 285
Thanks: 0
Thanked 0 Times in 0 Posts
MRMAN is an unknown quantity at this point
Thank You GJay. Sorry for putting you to trouble. I tried to open that link but it didn't work. (Works now though). that is exactly what i needed.

Thank you again.
MRMAN is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:38 PM.


Advertisement
Log in to turn off these ads.