MySQL Search - Google like?

Thread Solved

Join Date: Dec 2007
Posts: 112
Reputation: !Unreal is an unknown quantity at this point 
Solved Threads: 2
!Unreal's Avatar
!Unreal !Unreal is offline Offline
Junior Poster

MySQL Search - Google like?

 
0
  #1
Feb 26th, 2009
Ive got a MySQL search, which searches an index of urls. The search parameter searches titles. What I want to do is have something like Google.

So, if someone searches:

  1. site:example.com something to search

It would look for example.com in one column then from those results it would look for 'something to search' in another given column.

How would I do this?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: MySQL Search - Google like?

 
1
  #2
Feb 26th, 2009
Try looking at this page.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 112
Reputation: !Unreal is an unknown quantity at this point 
Solved Threads: 2
!Unreal's Avatar
!Unreal !Unreal is offline Offline
Junior Poster

Re: MySQL Search - Google like?

 
0
  #3
Feb 26th, 2009
How would I do it though? Im really lost...
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: MySQL Search - Google like?

 
0
  #4
Feb 26th, 2009
Do you already have the part of the query that matches the site? Add to that (with an AND clause) a MATCH(field) AGAINST('value') as demonstrated in the link I gave you.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 112
Reputation: !Unreal is an unknown quantity at this point 
Solved Threads: 2
!Unreal's Avatar
!Unreal !Unreal is offline Offline
Junior Poster

Re: MySQL Search - Google like?

 
0
  #5
Feb 26th, 2009
So would I do something like

  1. SELECT FROM table1 WHERE column1='$foo' IN table1 WHERE column1='$foo2'

Is that right? Just guess work :p
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,463
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: MySQL Search - Google like?

 
0
  #6
Feb 27th, 2009
Originally Posted by !Unreal View Post
So would I do something like

  1. SELECT FROM table1 WHERE column1='$foo' IN table1 WHERE column1='$foo2'

Is that right? Just guess work :p
For starters that mysql query is wrong and I shall share a way help answer this question. First to retrieve all site: within a string use the following:
  1. $string='test site:example.com site:test.com test2';
  2. if (preg_match('/site:/i',$string))
  3. {
  4. preg_match_all('/site:[^ ]+/i',$string,$site);
  5. for ($ii=0;isset($site[0][$ii]);$ii++)
  6. {
  7. $site[0][$ii]=strtolower(substr($site[0][$ii],5,strlen($site[0][$ii])));
  8. }
  9. $sites=$site[0]; unset($site);
  10.  
  11. //now to display it
  12. foreach ($sites AS $siteval)
  13. {
  14. echo $siteval."<br>";
  15. }
  16. }
That will turn it into the $sites array. The reason why an array is just in case there is more than one site specified. Then to do the mysql query it would be something like the following:
  1. mysql_query("SELECT * FROM `table` WHERE `domain`='".mysql_real_escape_string($sites[0])."'");
Last edited by cwarn23; Feb 27th, 2009 at 2:10 am.
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 112
Reputation: !Unreal is an unknown quantity at this point 
Solved Threads: 2
!Unreal's Avatar
!Unreal !Unreal is offline Offline
Junior Poster

Re: MySQL Search - Google like?

 
0
  #7
Feb 27th, 2009
Thats amazing, thank you so much. What I will have to do is to tell my users to set site:example.com first then put the query. To make it easier.

So, to get the query. Ive done this...
  1. $search = explode(' ', $string);
  2. echo $search[1];

It works but is pretty rudimentary, would you recomend something better?

EDIT: This is my final code. Im really tired now and there are probably loads of errors lol.

  1. $search= $_GET['q'];
  2. if (preg_match('/site:/i',$string))
  3. {
  4. preg_match_all('/site:[^ ]+/i',$string,$site);
  5.  
  6. for ($ii=0;isset($site[0][$ii]);$ii++)
  7. {
  8. $site[0][$ii]=strtolower(substr($site[0][$ii],5,strlen($site[0][$ii])));
  9. }
  10. $sites=$site[0];
  11. $query = explode(' ', $string);
  12.  
  13. $sql = "SELECT * FROM list WHERE title LIKE '%$query%' AND WHERE url LIKE '%$sites%'";
  14. }
  15. else {
  16. $sql = "SELECT * FROM list WHERE title LIKE '%$search%'";
  17. }
Last edited by !Unreal; Feb 27th, 2009 at 5:53 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,463
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: MySQL Search - Google like?

 
0
  #8
Feb 27th, 2009
Originally Posted by !Unreal View Post
Thats amazing, thank you so much. What I will have to do is to tell my users to set site:example.com first then put the query. To make it easier.

So, to get the query. Ive done this...
  1. $search = explode(' ', $string);
  2. echo $search[1];

It works but is pretty rudimentary, would you recomend something better?
Yes, using the explode function would be the best way to get all of the keywords but in your example, you have got the site:example.com as a keyword below is how I would do it.
  1. $string='test site:example.com site:test.com test2';
  2. if (preg_match('/site:/i',$string))
  3. {
  4. preg_match_all('/site:[^ ]+/i',$string,$site);
  5. for ($ii=0;isset($site[0][$ii]);$ii++)
  6. {
  7. $site[0][$ii]=strtolower(substr($site[0][$ii],5,strlen($site[0][$ii])));
  8. }
  9. $sites=$site[0]; unset($site);
  10. }
  11. $search=explode(' ',preg_replace('/[\h]+/',' ',preg_replace('/site:[^\h]+/i','',$string)));
  12.  
  13.  
  14. //now to display
  15. echo "<b>Sites</b><br>";
  16. foreach ($sites AS $siteval)
  17. {
  18. echo $siteval."<br>";
  19. }
  20. echo "<br><b>Keywords</b>";
  21. foreach ($search AS $val)
  22. {
  23. echo "<br>".$val;
  24. }
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 112
Reputation: !Unreal is an unknown quantity at this point 
Solved Threads: 2
!Unreal's Avatar
!Unreal !Unreal is offline Offline
Junior Poster

Re: MySQL Search - Google like?

 
0
  #9
Feb 27th, 2009
Hmm...doesnt seem to be working. Is this query valid?

  1. SELECT * FROM list WHERE title LIKE '%$search%' AND WHERE url LIKE '%$sites%'
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,463
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: MySQL Search - Google like?

 
0
  #10
Feb 27th, 2009
Originally Posted by !Unreal View Post
Hmm...doesnt seem to be working. Is this query valid?

  1. SELECT * FROM list WHERE title LIKE '%$search%' AND WHERE url LIKE '%$sites%'
The mysql query should look more like the following:
  1. SELECT * FROM list WHERE title LIKE '%$search%' AND url LIKE '%$sites%'
or for my preference
  1. mysql_query("SELECT * FROM `list` WHERE `title` LIKE '%".$search."%' AND `url` LIKE '%".$sites."%'");
Also note that $search and $sites must not be an array when used in the query. If you do use an array in a mysql query and you don't specify the exact value (example: $search[0]) Then it will just scan the database for the word 'Array'. So if $search and $sites are arrays then you may need to put the query inside a loop and for testing purposes you could use the following:
  1. mysql_query("SELECT * FROM `list` WHERE `title` LIKE '%".$search[0]."%' AND `url` LIKE '%".$sites[0]."%'");
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC