 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
09-29-2007, 09:06 PM
|
#1 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
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?
|
|
|
|
10-02-2007, 02:14 PM
|
#3 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
FULLTEXT is not supported in INNODB and foreign keys are not supported in MYISAM. :(
|
|
|
|
10-02-2007, 07:20 PM
|
#4 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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.
|
|
|
|
10-02-2007, 07:26 PM
|
#5 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
Did you read my post above?
|
|
|
|
10-02-2007, 07:34 PM
|
#6 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
You can't use %$keyword% ?
|
|
|
|
10-03-2007, 12:23 AM
|
#7 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
Quote:
Originally Posted by Tanax
You can't use %$keyword% ?
|
I cannot use the FULL TEXT searching query since my database is INNODB.
|
|
|
|
10-03-2007, 05:17 AM
|
#8 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
And what does that mean?
Can't you just say yes or no? >.<
Can you or can you not use %$search% ?
|
|
|
|
10-03-2007, 07:50 AM
|
#9 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
I can use the sprintf.
|
|
|
|
10-03-2007, 07:59 AM
|
#10 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
So use it then? :P And use %$search%
|
|
|
|
10-03-2007, 11:20 AM
|
#11 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Quote:
Originally Posted by Tanax
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.
|
|
|
10-03-2007, 08:18 PM
|
#12 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
What? Wildhoney is not gonna come with something better than my suggestion? :O W000t?!
This is my lucky day.. :D
|
|
|
|
10-03-2007, 10:20 PM
|
#13 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
10-04-2007, 09:56 AM
|
#14 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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" :)
|
|
|
|
10-04-2007, 11:34 AM
|
#15 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
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.
|
|
|
|
10-04-2007, 02:54 PM
|
#16 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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.
|
|
|
|
10-04-2007, 03:45 PM
|
#17 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
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.
|
|
|
|
10-04-2007, 10:31 PM
|
#18 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Ahh :P Sorry
|
|
|
|
10-06-2007, 01:03 PM
|
#19 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
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%);
|
|
|
|
10-06-2007, 02:22 PM
|
#20 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
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%");
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|