TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-29-2007, 09:06 PM   #1 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default How to make search functions?

I currently use MYSQL queries for searching data. How can I create a function which gets the closest data relative to the search term?
Haris is offline  
Reply With Quote
Old 09-29-2007, 09:44 PM   #2 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

Hope you don't mind a bit of reading. Check this out: http://dev.mysql.com/doc/refman/5.1/...xt-search.html
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 10-02-2007, 02:14 PM   #3 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

FULLTEXT is not supported in INNODB and foreign keys are not supported in MYISAM. :(
Haris is offline  
Reply With Quote
Old 10-02-2007, 07:20 PM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

PHP Code:
$pSql sprintf("SELECT * FROM ".$system->db->table['players']." WHERE name LIKE %1$s OR level LIKE %1$s OR vocation LIKE %1$s",
        %
$search%);    
        
$pResult $system->db->query($pSql); 
If I search for Ser, I will get Server, and Service and other words with "ser" in it, IF they exists in the database ofcourse.
Tanax is offline  
Reply With Quote
Old 10-02-2007, 07:26 PM   #5 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Did you read my post above?
Haris is offline  
Reply With Quote
Old 10-02-2007, 07:34 PM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

You can't use %$keyword% ?
Tanax is offline  
Reply With Quote
Old 10-03-2007, 12:23 AM   #7 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
You can't use %$keyword% ?
I cannot use the FULL TEXT searching query since my database is INNODB.
Haris is offline  
Reply With Quote
Old 10-03-2007, 05:17 AM   #8 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

And what does that mean?
Can't you just say yes or no? >.<

Can you or can you not use %$search% ?
Tanax is offline  
Reply With Quote
Old 10-03-2007, 07:50 AM   #9 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

I can use the sprintf.
Haris is offline  
Reply With Quote
Old 10-03-2007, 07:59 AM   #10 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

So use it then? :P And use %$search%
Tanax is offline  
Reply With Quote
Old 10-03-2007, 11:20 AM   #11 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
So use it then? :P And use %$search%
Definitely the way to do it if you're looking for a string in a string. You also have regex, but unless the above proves too basic, then regex is too complex.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-03-2007, 08:18 PM   #12 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

What? Wildhoney is not gonna come with something better than my suggestion? :O W000t?!

This is my lucky day.. :D
Tanax is offline  
Reply With Quote
Old 10-03-2007, 10:20 PM   #13 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Haha. No, no, you seem to have gone about it the right way for what he wants :) !
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-04-2007, 09:56 AM   #14 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Haha, thanks :)


btw, Haris..

% is a like.. a universal mark, that basicly means "anything".
So you search for anything-$searchword-anything.

So if you search for ape, you would get the results
ape
apes
tape
rape
mapex

etc..

That's why they are good ;)
Also if you search a database, the database "post" would be a string, so you if you don't use the %, you have to search for the whole post to get a result.

Let's say you have a news post with this:
"Once upon a time there was an ape..."

If you don't use %, and search for ape, you don't get anything, cuz you have to search for "Once upon a time there was an ape" to a result.
IF you use, you would get the result "ape" and the newspost "once upon a time there was an ape" :)
Tanax is offline  
Reply With Quote
Old 10-04-2007, 11:34 AM   #15 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

To to tie things up a little bit, here's some more information that you might like to know (or might at least entertain reading my post for). The percent symbol (%) in this case is a wildcard character meaning essentially 'zero or more characters'. To build on the above posts, searching for '%ape%' would match results such as ape, aperitif, tape, psychotherapeutic to name a few. This can be a good feature or bad, depending on the circumstances. Generally people know the word that they want to search for and in this case it would be better to only bring back records with the discrete word ape, rather than records with words containing the letters ape in them. Achieving this won't be covered in this post, but instead I'll move on to what I originally wanted to mention.

As well as the percent wildcard, you can also use an underscore (_) which is another wildcard but behaves differently. Like the percent, it matches any character but this time it will match one, and only one character, rather than 'zero or more'. To give an example, using '_ape' would match records; cape, tape but not lapel, paper, apes, drape, 'blue cape' and so on.

Last edited by Salathe : 10-04-2007 at 03:46 PM.
Salathe is offline  
Reply With Quote
Old 10-04-2007, 02:54 PM   #16 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Yea but salethe, if you search in a database row, the whole content of that row is a string.

Lorem ipsum something blablabla aye sit bla hey! que?

If you search for aye, you won't get any result, because there IS something before aye aswell as after.
So, you won't get any results from that.

That's why you need % on both sides of the search term.
Tanax is offline  
Reply With Quote
Old 10-04-2007, 03:45 PM   #17 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I'm not here to disagree with your post, just to give a fuller understanding of what can and can't be done. In most cases, using the % wildcard around your keyword(s) is what you'll want.
Salathe is offline  
Reply With Quote
Old 10-04-2007, 10:31 PM   #18 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Ahh :P Sorry
Tanax is offline  
Reply With Quote
Old 10-06-2007, 01:03 PM   #19 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Thanks for the help!

It worked.

Edit:

Getting unexpected % error:

PHP Code:
            $szSQL sprintf("SELECT user,id,rank FROM users WHERE user LIKE '%s' ORDER BY id", %$szUsername%); 
Haris is offline  
Reply With Quote
Old 10-06-2007, 02:22 PM   #20 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Hey, you need to encapsulate the percentage signs within your string. Change this:

PHP Code:
$szSQL sprintf("SELECT user,id,rank FROM users WHERE user LIKE '%s' ORDER BY id", %$szUsername%); 
to this:

PHP Code:
$szSQL sprintf("SELECT user,id,rank FROM users WHERE user LIKE '%s' ORDER BY id""%$szUsername%"); 
Karl is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:15 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design