IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet

Searching With Php And Mysql

, The easy way :P


bigfatme2000
no avatar
Newbie [Level 2]
**
Group: Members
Posts: 34
Joined: 28-October 05
Member No.: 13,491



Post #1 post Jan 12 2006, 05:59 PM
Searching with PHP and MySQL is pretty easy when you think about it, especially if you're doing it the simple way (without boolean or whatever) tongue.gif

It consists of a few forms, a query and an output. As I said, simple!

CODE
<form name=\"form1\" id=\"form1\" method=\"post\" action=\"<? $php_self ?>\">
<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\">
<tr>
<td style=\"text-align: center;\">
<input name=\"search\" type=\"text\" id=\"search\" />
</td>
</tr>
<tr>
<td style=\"text-align: center;\">
<input name=\"submit\" type=\"submit\" id=\"submit\" value=\"Search!\" />
</td>
</tr>
</table>
</form>


The form that you just made contains a text input that people will type into, the next code will extract the form data and then search it..

Form processing wink.gif


CODE
$search = htmlspecialchars(addslashes($_POST[\\\'search\\\']), ENT_QUOTES);
if(isset($_POST[\\\'submit\\\'])) {
if(!$search) {
echo \\\"No search entered, please go back and fill in the fields properly.\\\";
}
else {
$query = mysql_query(\\\"SELECT * FROM table WHERE field LIKE \\\'%$search%\\\'\\\");
$resultnum = mysql_num_rows($query); // Just print $resultnum if you want to show how many results returned

if($resultnum>0) { // Echos out matches if anything was found

while($row=mysql_fetch_array($query)) { // Starts spitting out the data

echo \\\"You can put a link to the result article here, or something else. This is what\\\'ll show up when a user gets a result, for each result.<br />\\\"

}
}
}
}
else {
echo \\\"<p>No search entered?</p>\\\";
}



And that's it. Let me just break this down for you wink.gif
htmlspecialchars(addslashes:
This line does two things; it cleans out any chance of an SQL injection to your database, and then it adds slashes to the value which makes the search more accurate. If you don't add slashes, then if you searched for cheese, it would return anything with a c, h, e or s in your database. So much for search relavance, eh? laugh.gif

$query = mysql_query(\\\"SELECT * FROM table WHERE field LIKE \\\'%$search%\\\'\\\"):
This is the query that does the search. Notice the percentage signs in the LIKE part - don't forget these. They work as wildcards.

And the rest I'm sure you'll understand. biggrin.gif rolleyes.gif
Go to the top of the page
+Quote Post
 
Start new topic
Replies (1 - 2)
Tenshigami
no avatar
Newbie [Level 1]
*
Group: Members
Posts: 10
Joined: 29-January 06
Member No.: 17,862



Post #2 post Jan 29 2006, 05:23 PM
=D wow very usefull! Thanks, I think Ill try it out now! =_= realy easy to fallow to.
Go to the top of the page
+Quote Post
Amezis
no avatar
Privileged Member
*********
Group: Members
Posts: 535
Joined: 14-February 05
From: Oslo, Norway
Member No.: 3,759



Post #3 post Jan 29 2006, 05:58 PM
AWESOME TUTORIAL, I was just looking for this!!! Thanks alot, I hope it will work biggrin.gif

Thanks once again tongue.gif

(But why all those \\\ everywhere?)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts 3 chalawanonline 6,297 2nd August 2004 - 02:34 PM
Last post by: Spectre
No New Posts   7 Raptrex 13,236 27th August 2004 - 11:16 PM
Last post by: Spectre
No New Posts 4 NuHoaXuLa 6,284 29th August 2004 - 03:12 AM
Last post by: NuHoaXuLa
No New Posts   11 Triple X 6,381 5th September 2004 - 02:33 AM
Last post by: Triple X
No New Posts   4 Schweden 4,988 8th September 2004 - 09:40 AM
Last post by: Schweden
No New Posts   2 Too_Hot 3,624 16th September 2004 - 05:25 PM
Last post by: serverph
No New Posts   2 BoSZ 4,260 17th September 2004 - 09:09 PM
Last post by: BoSZ
No New Posts   7 XtremeGamer99 6,864 18th December 2008 - 10:13 PM
Last post by: minimcmonkey
No New Posts   8 guangdian 5,607 2nd October 2004 - 05:03 AM
Last post by: football123213
No New Posts   5 JaVe 4,846 25th December 2008 - 10:11 AM
Last post by: xpress
No New Posts   3 outwarsecrets 4,761 10th October 2004 - 08:04 AM
Last post by: whatknows
No New Posts   1 farsiscript 937 9th November 2009 - 12:26 AM
Last post by: kittycatlover763
No New Posts   8 Becca 4,878 16th October 2004 - 10:22 PM
Last post by: Becca
No New Posts 3 pbrugge 3,718 18th October 2004 - 11:41 PM
Last post by: pbrugge
No New Posts   4 dundun2007 9,212 19th December 2008 - 03:53 AM
Last post by: liod


 



RSS Open Discussion Time is now: 26th November 2009 - 06:05 PM

Web Hosting Powered by ComputingHost.com. Xisto.com : Honesty ROCKS! Truth Rules.