Current location: Hot Scripts Forums » Other Discussions » Database » Database select querry


Database select querry

Reply
  #1 (permalink)  
Old 11-20-09, 06:14 PM
alane alane is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 71
Thanks: 6
Thanked 0 Times in 0 Posts
Database select querry

Hi
I have a working database and search facility that I need to refine a bit but am having problem with
.
PHP Code:

$query "SELECT 

    model, 
    brand, 
    gc_number, 
    dir, 
    manuals_pdffiles.id, 
    pdf_file, 
    category, 
    pdf_info, 
    DATE_FORMAT(date_available, '%d %b %y') AS Fdate, 
    safety, 
    asbestos,
    class 
FROM 
    manuals_models 
LEFT JOIN 
    manuals_pdffiles ON manual_id = manuals_pdffiles.id 
LEFT JOIN 
    manuals_brands ON brand_id = manuals_brands.id 
WHERE 
    (model like '%
$entry%') AND class = 1 
ORDER BY 
    brand, model "

The field 'models' might contain an entry such as "Potterton Puma 80e" and if the site visitor enters a search string ($entry) enters say "puma 80e" the select statement returns the correct entry. If they enter "puma 80 e" it doesn't obviously.

I can easily modify the search string $entry to remove all the spaces but how do I modify the WHERE statement so that the database entry 'model' is also minus any spaces?

Using the above example then 'model' would end up as "PottertonPuma80e" and the search string would be "puma80e". Then if site visitor added extra spaces (or left any out) the select statement would find the entry?

Hope that makes sence!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 11-20-09, 11:17 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Let's assume that you have already used str_replace() on $entry to remove all the spaces.
PHP Code:

$entry str_replace(" ","",$entry); 

And now you want to remove all the spaces from the model field before you use it in the query.
Sounds funny, but that's kinda what you have to do, only you can do it right in the query
PHP Code:

$query "SELECT
    model,
    brand,
    gc_number,
    dir,
    manuals_pdffiles.id,
    pdf_file,
    category,
    pdf_info,
    DATE_FORMAT(date_available, '%d %b %y') AS Fdate,
    safety,
    asbestos,
    class
FROM
    manuals_models
LEFT JOIN
    manuals_pdffiles ON manual_id = manuals_pdffiles.id
LEFT JOIN
    manuals_brands ON brand_id = manuals_brands.id
WHERE
    (REPLACE(model,' ','') like '%
$entry%') AND class = 1
ORDER BY
    brand, model "

__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to job0107 For This Useful Post:
alane (11-21-09)
  #3 (permalink)  
Old 11-21-09, 12:44 PM
alane alane is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 71
Thanks: 6
Thanked 0 Times in 0 Posts
Thanks for that. It was neat and really works a treat.

My site has several different search facilities and having seen your reply would like to ask is there a way that I can include in a select statement a way of only comparing the alpha numeric part of a columns content (which includes - & / symbols) such as the mysql equivilent of the following in php?
PHP Code:

$entry ereg_replace("[^A-Za-z0-9]"""$entry); 

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 11-23-09, 08:11 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I am sorry, but there is no easy way to do a regular expression replace with MySql.
Maybe they will add it in the future.
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 11-23-09, 12:36 PM
alane alane is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 71
Thanks: 6
Thanked 0 Times in 0 Posts
Thanks for taking time to help me here.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 11-23-09, 09:52 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I suppose that if you have only a few symbols like " - , / , & , space",
then you could use a nested REPLACE and remove each symbol one at a time.

Example:

This example will remove all dashes ( - ), forward slashes ( / ), and-signs ( &) and spaces " ".

The inner REPLACE replaces all spaces.
The next outer REPLACE replaces all and-signs.
The next outer REPLACE replaces all forward slashes.
And the last outer REPLACE replaces all dashes.

Each preceding REPLACE is used as the object for each successive REPLACE.
I am not sure how many levels can be nested, but I am sure it's quite a few.

PHP Code:

$query "SELECT
    model,
    brand,
    gc_number,
    dir,
    manuals_pdffiles.id,
    pdf_file,
    category,
    pdf_info,
    DATE_FORMAT(date_available, '%d %b %y') AS Fdate,
    safety,
    asbestos,
    class
FROM
    manuals_models
LEFT JOIN
    manuals_pdffiles ON manual_id = manuals_pdffiles.id
LEFT JOIN
    manuals_brands ON brand_id = manuals_brands.id
WHERE
    (REPLACE(REPLACE(REPLACE(REPLACE(model,' ',''),'&',''),'/',''),'-','') like '%
$entry%') AND class = 1
ORDER BY
    brand, model "

__________________
Jerry Broughton

Last edited by job0107; 11-23-09 at 10:04 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to job0107 For This Useful Post:
alane (11-25-09)
  #7 (permalink)  
Old 11-24-09, 10:20 AM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
Trophee

Code:
(REPLACE(REPLACE(REPLACE(REPLACE(model,' ',''),'&',''),'/',''),'-','') like '%$entry%')
I think parts of my brain just melted. Had never seen anything done in MySQL that gross before. Course it is a solution.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 11-25-09, 04:29 AM
alane alane is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 71
Thanks: 6
Thanked 0 Times in 0 Posts
Quote:
Had never seen anything done in MySQL that gross before. Course it is a solution.
That may be true, but tried it out last evening and the best bit is IT WORKS!!

I might over the next few days change the enteries in the actual table so that I only have to nest the REPLACE 2 levels but this use of the REPLACE achives exactly what I wanted - Thanks.

It certainly helps solve a problem that doesn't seem otherwise possible in MYsql.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 11-25-09, 05:23 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by carters-site View Post
Code:
(REPLACE(REPLACE(REPLACE(REPLACE(model,' ',''),'&',''),'/',''),'-','') like '%$entry%')
I think parts of my brain just melted. Had never seen anything done in MySQL that gross before. Course it is a solution.
Well, if you think you can come up with a better solution for this particular situation, I would be very interested in seeing it.

Otherwise your criticism is just so much hot air.

Thanks for nothing.
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 11-25-09, 07:17 AM
alane alane is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 71
Thanks: 6
Thanked 0 Times in 0 Posts
I don't care what others think.

I came with a problem that couldn't think of a way of solving and came here as I have done previously when I hit problems with various scripts. By trade I repair household boilers and whilst I might be good at that I struggle with PHP, databases etc.

job came up with a solution that works, also a welcome explaination of how, and has helped improve the functionality of my site no end. To say I am happy is an understatement.

Maybe 'carters-site' might consider it gross but just maybe they are just a tad jelous they never thought of the idea.

Again job0107 (Jerry?) my thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me on How to Select Records from the database airprince Visual Basic 2 08-16-06 03:06 AM
select variable from one database to use in another?!?? Deansatch PHP 1 07-07-06 03:24 PM
Dynamic select box from database values PrashantJsp JavaScript 2 05-04-05 01:29 AM
PHP Can't select database EST PHP 4 08-19-03 11:14 AM


All times are GMT -5. The time now is 01:59 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.