Be a Supporter!

Php: Search Engine

  • 3,183 Views
  • 33 Replies
New Topic Respond to this Topic
Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Php: Search Engine 2006-01-09 05:34:18 Reply

PHP: Main

Level: Intermediate
Category: Specific Projects
Notes: '*' are my personal notes, it's suggested to delete them if you're copying this.

- - -

After I saw the success of the PHP: Main thread I thought it was time to also contribute something to it. But what is really useful? Something what is frustrating loads of people and hasn't been explained yet. I bring to you: your own little search engine!
First off: you need to have some PHP and MySQL knownledge, else you will understand what most of it does. I kept this tutorial as simple as possible, so most people are able to understand it and so it's ready for being used.

What does this search engine?
It allows you to search through a certain table for a specific name - in this tutorial is allows you to search for a user is a user table. This tutorial will help you to optimize your search results. Still, it has been kept as simple as possible, so everyone is able to use it and/or use pieces of it.

Where can it be used for?
For pretty much everything that is too big to manually dive in to and it allows other people to even search for certain things - if you allow them ofcourse.

- To get started -
I'm letting search this script in three types: begins with, contains and ends with. Here's the main search form:

<form action="search.php" method="get">
<select name="t"> * t for type
<option value="a">Begins with</option> * a = begins with
<option value="b">Contains</option> * b = contains
<option value="c">Ends with</option> * c = ends with
</select>
<input type="text" name="term"><br>
<input type="submit" value="Search"><br>
</form>

- PHP Code -

After submitting the form I suggest you're adding something that makes it more secure.

<?PHP

$type = $_GET['t'];
$term = $_GET['term'];

# Just a small protection.
$term = strip_tags($term);

# They have searched.
if ($term) {

if (strlen($term) < 3) {
echo 'Your search term needs to be longer than three characters.<br>';
exit;
}

if (strlen($term) > 16) {
echo 'Our usernames are not longer than 16 characters.';
exit;
}

* Now here's the trick:

# Get the SQL command by the search type.
if($type == 'a') {
$sql = "SELECT * FROM user_table WHERE username LIKE '$term%'";
} elseif($type == 'b') {
$sql = "SELECT * FROM user_table WHERE username LIKE '%$term%'";
} elseif($type == 'c') {
$sql = "SELECT * FROM user_table WHERE username LIKE '%$term'";
}

* Using '%' it will begin searching where the % has been placed. Quite simple, but it's effective in getting search results. Now you might want to limit the search results if you're having a user table which contains a lot of members.

$query = mysql_query($sql);
* Now query will seek the data using $sql.

$rows = mysql_num_rows($query);
* We also need to be sure if somethig could be found.

if ($rows == 0) {
echo 'No such term ('.$term.') could be found in our user database.';
exit;
}

* Nothing could be found, show an error message and stop the script.

-- END OF PART I --

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Response to Php: Search Engine 2006-01-09 05:35:23 Reply

Now I'm sorry I needed to split it, but else it would not let me upload - for some odd reason.

-- PART II --

- Loop the results -

echo 'Found '.$rows.' possibly results.<br><hr><br>';

while ($info = mysql_fetch_array($query)) {

$count = $count + 1;
echo ''. $count .'. ' . USER_PROFILE_LINK .'<br>';
}

* The $count-variable is just something smalls, but I like it. :)
* Now comes something that is quite important, because it needs to be closed, so do not forget to add a '}' in the end.

} # End for if ($term) {

- At the end -

This is something what I've done, but it isn't needed, it's just what you want, but it might be even easier to use this aswell, in the PHP code:

if (empty($term) && !$term) {

echo '<form action="' . $PHP_SELF . '" method="get">
<select name="t"> * t for type
<option value="a">Begins with</option> * a = begins with
<option value="b">Contains</option> * b = contains
<option value="c">Ends with</option> * c = ends with
</select>
<input type="text" name="term"><br>
<input type="submit" value="Search"><br>
</form>';

}

* Nothing has been searched or the term field has been left empty, so display the form again.

* Now that would be it! Just add a close tage and you're done.

?>

And we're done.

Specia Notes
This has been tested and it's fully working. You can find an exmaple here. Just to even make it easier; I've added three links containing the three search types, who're working:

1. Search for 'zen' (begins with)
2. Search for 'oof' (contains)
3. Search for 'erkt' (ends with)

Links:
Search Source
Working version

Now if there might be problems/questions/complaintments I would love to hear them all! :)
I hoped this tutorial contributed something to one of your futher projects.

Claxor
Claxor
  • Member since: Oct. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to Php: Search Engine 2006-01-09 05:46:54 Reply

Nice tutorial =)


BBS Signature
Sir-Davey
Sir-Davey
  • Member since: Jul. 9, 2001
  • Offline.
Forum Stats
Supporter
Level 19
Blank Slate
Response to Php: Search Engine 2006-01-09 10:54:05 Reply

That is really nice!

Hmm, I wonder if it would be a good idea to post something about searching static pages with PHP. That is, have a crawler bot, and a searching script. Hmmmm. *thinks about it*


BBS Signature
Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Response to Php: Search Engine 2006-01-09 10:57:55 Reply

At 1/9/06 05:46 AM, Claxor wrote: Nice tutorial =)

Thank you. :)

At 1/9/06 10:54 AM, Sir-Davey wrote: That is really nice!

And thank you.

Hmm, I wonder if it would be a good idea to post something about searching static pages with PHP. That is, have a crawler bot, and a searching script. Hmmmm. *thinks about it*

I'll leave that up to you. ;)

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Search Engine 2006-01-10 14:59:51 Reply

Nice one, but I think this one should into expert section.

There's some really complicated stuff...

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Response to Php: Search Engine 2006-01-10 15:22:17 Reply

At 1/10/06 02:59 PM, Nino_JoJ wrote: Nice one, but I think this one should into expert section.

We don't have an expert category, but I think it might be there, because there are some other tutorials who are quite hard to understand - not because of what has been written, but the content.

There's some really complicated stuff...

If many people think there is it should be an higher category - if possible.

EviLDoG
EviLDoG
  • Member since: Oct. 18, 2000
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to Php: Search Engine 2006-01-10 16:08:45 Reply

Also, never LEFT JOIN on search results, grab the id's from the search results then left join like:
$rowexe = mysql_query ("SELECT id FROM t1 WHERE username LIKE ('%".$term."%')") or die ('Could not process query');
while ( $row = mysql_fetch_row ( $rowexe ) ) {
$ids[] = $row[0];
}

if ( !is_array ($ids) ) {
echo "Could not find any matching results";
} else {
$userexe = mysql_query (
"SELECT t1.*, t2.*, t3.*
FROM t1
LEFT JOIN t2 ON t1.id=t2.user_id
LEFT JOIN t3 ON t2.id=t3.review_id
WHERE t1.id IN (".implode( ', ', $ids ).")");
// then just loop through as normal
}

i think thats how it did it anyway, it can shave like 30 secs off search time and reduce the strain on the cpu and mysql in serious cases.

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Response to Php: Search Engine 2006-01-11 09:08:03 Reply

At 1/10/06 04:08 PM, EviLDoG wrote: Also, never LEFT JOIN on search results, grab the id's from the search results then left join like:

Not that I did it in this tutorial...

i think thats how it did it anyway, it can shave like 30 secs off search time and reduce the strain on the cpu and mysql in serious cases.

You're adding another query. The query supposed to loop so you're having two queries which does make it slower than my code. I only use one and it gives back good results. Also, I suggest add a mysql_num_rows in there, so people know how many results have been grabbed. :)

Sir-Davey
Sir-Davey
  • Member since: Jul. 9, 2001
  • Offline.
Forum Stats
Supporter
Level 19
Blank Slate
Response to Php: Search Engine 2006-01-11 12:03:17 Reply

You'd think that LEFT JOINing would be much faster on high traffic sites, but it turns out to be the opposite. Making more smaller queries actually reduces latency. At least that's what the guy from gaiaonline has noticed.


BBS Signature
Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to Php: Search Engine 2006-01-11 12:37:51 Reply

The only thing I'd suggest is using a switch statement instead of a series of if statements on one variable.

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Response to Php: Search Engine 2006-01-11 13:48:34 Reply

At 1/11/06 12:03 PM, Sir-Davey wrote: You'd think that LEFT JOINing would be much faster on high traffic sites, but it turns out to be the opposite. Making more smaller queries actually reduces latency. At least that's what the guy from gaiaonline has noticed.

Yes, you're correct. EvilDogs' script is only asking the same information twice. And my script returns very good results, so it works good enough. And do not forget, this is ment to kept easy - so most people would understand it.

At 1/11/06 12:37 PM, Pilot-Doofy wrote: The only thing I'd suggest is using a switch statement instead of a series of if statements on one variable.

Yes, that's something I made have should done - it makes it a lot faster. :)
Thanks for the tip!

EviLDoG
EviLDoG
  • Member since: Oct. 18, 2000
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to Php: Search Engine 2006-01-13 06:21:31 Reply

At 1/11/06 09:08 AM, Zendra wrote:
At 1/10/06 04:08 PM, EviLDoG wrote: Also, never LEFT JOIN on search results, grab the id's from the search results then left join like:
Not that I did it in this tutorial...

i think thats how it did it anyway, it can shave like 30 secs off search time and reduce the strain on the cpu and mysql in serious cases.
You're adding another query. The query supposed to loop so you're having two queries which does make it slower than my code. I only use one and it gives back good results. Also, I suggest add a mysql_num_rows in there, so people know how many results have been grabbed. :)

I was just adding a bit of useful information, i was not criticising your tutorial or method.

And about the speed, it depends on the circumstances, if indeed you are left joining, my ways quicker, but if you aren't, your way is quicker. Once again, i was no criticising your method, you just assumed i was.

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Response to Php: Search Engine 2006-01-13 06:34:23 Reply

At 1/13/06 06:21 AM, EviLDoG wrote: I was just adding a bit of useful information, i was not criticising your tutorial or method.

And about the speed, it depends on the circumstances, if indeed you are left joining, my ways quicker, but if you aren't, your way is quicker. Once again, i was no criticising your method, you just assumed i was.

I know you're not critiscising my tutorial. :)
As Sir-Davey also said left joining can make it slower if has the process a lot of information. Like I said before you're adding another query which makes the process overall slower. Your technique is good if you want to bring up 100% correct results. Mine is for showing a list of possible results. :)

Then again, it's nice of you to add it, so there's always an alternative or a modification way.

EviLDoG
EviLDoG
  • Member since: Oct. 18, 2000
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to Php: Search Engine 2006-01-13 06:41:31 Reply

At 1/13/06 06:34 AM, Zendra wrote: I know you're not critiscising my tutorial. :)
As Sir-Davey also said left joining can make it slower if has the process a lot of information. Like I said before you're adding another query which makes the process overall slower. Your technique is good if you want to bring up 100% correct results. Mine is for showing a list of possible results. :)

It's not for 100% correct results, it's for returning more information in results (For example in a forum where you wanted to return the forum name, topic name and username)
If you were to use 1 query while left joining on something like that, it would make it hellishly slower (If that's even a word). I'm not sure if anyone here has actually dealt with left joining on search queries, but if they have they will tell you 2 queries is quicker than 1 when left joining (I think that's sort of what sir-davey was saying).

But as far as left joining goes when comes to making it slower... it doesn't matter if you do it right and use the extra query.

Then again, it's nice of you to add it, so there's always an alternative or a modification way.

It's more like an in depth addition as opposed to a modification or alternative :).

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Search Engine 2006-02-16 12:15:23 Reply

Warning: mysql_connect(): Access denied for user: 'mustywin@localhost' (Using password: YES) in /home/mustywin/large_template.php on line 64
Could not connect to MySQL server. MySQL: Access denied for user: '@localhost' to database 'mustywin_view'Could not select MySQL database. MySQL: Access denied for user: '@localhost' to database 'mustywin_view'
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mustywin/large_template.php on line 77

Fatal error: Call to undefined function: do_header() in /home/mustywin/large_template.php on line 230

Well, thanks anyway.

Sir-Davey
Sir-Davey
  • Member since: Jul. 9, 2001
  • Offline.
Forum Stats
Supporter
Level 19
Blank Slate
Response to Php: Search Engine 2006-02-16 12:23:52 Reply

At 2/16/06 12:15 PM, Nino_JoJ wrote: Warning: mysql_connect(): Access denied for user: 'mustywin@localhost' (Using password: YES) in /home/mustywin/large_template.php on line 64
Could not connect to MySQL server. MySQL: Access denied for user: '@localhost' to database 'mustywin_view'Could not select MySQL database. MySQL: Access denied for user: '@localhost' to database 'mustywin_view'
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mustywin/large_template.php on line 77

Fatal error: Call to undefined function: do_header() in /home/mustywin/large_template.php on line 230

Well, thanks anyway.

I just have ONE thing to say: LMAO

Hey, Einstein, you need to change the username and password to connect. Didn't you say you were the best PHP programmer in Croatia or something? Yeah... hum. Ok.


BBS Signature
Sir-Davey
Sir-Davey
  • Member since: Jul. 9, 2001
  • Offline.
Forum Stats
Supporter
Level 19
Blank Slate
Response to Php: Search Engine 2006-02-16 12:25:18 Reply

At 2/16/06 12:23 PM, Sir-Davey wrote: I just have ONE thing to say: LMAO

Hey, Einstein, you need to change the username and password to connect. Didn't you say you were the best PHP programmer in Croatia or something? Yeah... hum. Ok.

Uh-oh. I just made an ass of myself.

Move along, nothing to see here.


BBS Signature
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Search Engine 2006-02-16 12:27:24 Reply

At 2/16/06 12:25 PM, Sir-Davey wrote:
At 2/16/06 12:23 PM, Sir-Davey wrote: I just have ONE thing to say: LMAO

Hey, Einstein, you need to change the username and password to connect. Didn't you say you were the best PHP programmer in Croatia or something? Yeah... hum. Ok.
Uh-oh. I just made an ass of myself.

Move along, nothing to see here.

lol just thinking that


- Matt, Rustyarcade.com

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Response to Php: Search Engine 2006-02-16 12:48:19 Reply

At 2/16/06 12:15 PM, Nino_JoJ wrote: Well, thanks anyway.

If you just had try to access the main page of MustyWindows:

"February 16, 2006

We are currently switching servers to offer a faster and more reliable experience while browsing Mustywindows. Please bare with us as the global DNS servers update and the new site is available to everyone. Until we get all the errors worked out internally this will be the new homepage.

Expect the new site up by later this afternoon or on the 17th of February, 2006 at the latest. The backup file is hella big so we're doing the best we can. Stay in tune!"

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Search Engine 2006-02-24 12:55:51 Reply

At 2/16/06 12:48 PM, Zendra wrote: the pilot's message

I have seen that, but too late, though.

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Response to Php: Search Engine 2006-02-25 04:23:20 Reply

At 2/24/06 12:55 PM, Nino_JoJ wrote: I have seen that, but too late, though.

I'm awear that the files are currently offline. But because we also switched hosts I need tor re-configure them a bit. I shall put them online as soon as I get the change.
But then again, you can just build the code and it will work anyway. :)

Casualty
Casualty
  • Member since: Sep. 15, 2004
  • Offline.
Forum Stats
Member
Level 35
Musician
Response to Php: Search Engine 2006-02-25 11:45:59 Reply

I might use that once... Thanks!

henke37
henke37
  • Member since: Sep. 10, 2004
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Php: Search Engine 2006-02-26 03:55:20 Reply

Security, please!
You can't just allow the user to include unfiltered code.


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Response to Php: Search Engine 2006-02-26 05:22:18 Reply

At 2/26/06 03:55 AM, henke37 wrote: Security, please!
You can't just allow the user to include unfiltered code.

What are you talking about? I should tell them to secure information when inserting it into the database?
Why should I tell them to check certain things first, if they need to secure it in the first place when submitting it? Checking things twice is kinda pointless, don't you think?

henke37
henke37
  • Member since: Sep. 10, 2004
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Php: Search Engine 2006-02-27 05:19:22 Reply

I mean that you can't allow unfiltred indata to be passed to a database query.


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.

WoogieNoogie
WoogieNoogie
  • Member since: Jun. 26, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Php: Search Engine 2006-02-27 18:32:26 Reply

At 2/27/06 05:19 AM, henke37 wrote: I mean that you can't allow unfiltred indata to be passed to a database query.

Isn't that what the strip_tags() function is for?

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Response to Php: Search Engine 2006-03-04 04:39:12 Reply

Sorry it took a while but I managed to get the files up and running so you can view the source again and search MustWindows' user database for testing.
I know it's nothing big but I guess it might be demostrating what this is simple search engine is capable of. :)

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Response to Php: Search Engine 2006-03-04 04:58:40 Reply

Update:
I've added some more protection. If no search type has been specified or it's not a valid search type, it will return an error and stop.
You can view the search engine source, but also paste this code:

Put the code right under:
# They have searched.
if ($term) {

The code is looking as following:
$allowed_types = array('a', 'b', 'c');

foreach ($allowed_types as $types) {
if ($types == $type) {
$valid_type = true;
}
}

if ($valid_type !== true || empty($type)) {
echo 'Invalid search type.';
exit;
}

If there are some problems with this little modification please let me know and I shall see what I can do, as you can in the update version it works. Here's a little example of the code if you try to hack it: http://www.mustywind..S&term=IMHACKING

I'm also quite curious if some people are actuallu using this code. :)

henke37
henke37
  • Member since: Sep. 10, 2004
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Php: Search Engine 2006-03-04 06:34:25 Reply

I would do it this way:
$methods=array('a'=>true,'b'=>true,'c'=>tr
ue);
$isin=isset($methods[$search]);


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.