Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Searching With Php And Mysql, The easy way :P
bigfatme2000
post Jan 12 2006, 05:59 PM
Post #1


Newbie [Level 2]
**

Group: Members
Posts: 34
Joined: 28-October 05
Member No.: 13,491



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
Tenshigami
post Jan 29 2006, 05:23 PM
Post #2


Newbie [Level 1]
*

Group: Members
Posts: 10
Joined: 29-January 06
Member No.: 17,862



=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
post Jan 29 2006, 05:58 PM
Post #3


Privileged Member
*********

Group: Members
Posts: 535
Joined: 14-February 05
From: Oslo, Norway
Member No.: 3,759



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

Topics Topics
  1. Complete Login System(57)
  2. Shoutbox, Made Easy(17)
  3. How To Host Ur Own Site In 2 Mins Php+mysql Needed(34)
  4. Complete Login And Registration System(9)
  5. Php/mysql Login/register(2)
  6. Backing Up And Restoring Mysql Databases(10)
  7. Php Dynamic Signatures(9)
  8. Mysql Database Setup(1)
  9. A Nice Mysql Server Check(4)
  10. Install Php 5 To Work With Mysql(0)
  11. Advanced Google Searching(13)
  12. Installing Php + Mysql + Apache + Phpmyadmin On Windows Part 2(0)
  13. Quiz With Php, But Without Mysql(3)
  14. Check Referrer To Prevent Linking Yours From Other Sites(8)
  15. Starting Or Stopping Apache And Mysql Server Via Batch File(0)
  1. Cpanel Mysql Database Management(6)
  2. Simple Shoutbox(34)
  3. Simple User System(19)
  4. Adding Data To A Database And Displaying It Later(1)
  5. Getting Started With Mysql(2)


 



- Lo-Fi Version Time is now: 22nd November 2008 - 08:40 PM