<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Team Tutorials &#187; search</title>
	<atom:link href="http://teamtutorials.com/tag/search/feed" rel="self" type="application/rss+xml" />
	<link>http://teamtutorials.com</link>
	<description></description>
	<lastBuildDate>Tue, 27 Dec 2011 18:58:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Creating a Form that will Search a MySQL Database</title>
		<link>http://teamtutorials.com/web-development-tutorials/php-tutorials/creating-a-form-that-will-search-a-mysql-database?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=creating-a-form-that-will-search-a-mysql-database</link>
		<comments>http://teamtutorials.com/web-development-tutorials/php-tutorials/creating-a-form-that-will-search-a-mysql-database#comments</comments>
		<pubDate>Wed, 16 Jul 2008 08:29:58 +0000</pubDate>
		<dc:creator>John Ward</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://teamtutorials.com/?p=1601</guid>
		<description><![CDATA[In this tutorial I will show you how to add a simple search box to query a MySQL database. This tutorial is a continuation of the <a href="http://teamtutorials.com/web-development-tutorials/how-to-access-a-mysql-database-using-php">“How to Access a MySQL Database Using PHP”</a> tutorial.
Since writing that original tutorial I have created a<a href="http://teamtutorials.com/web-development-tutorials/setting-up-a-wamp-server"> local WAMP test environment</a>. I have created the database “test” with a table “testable”. The table contains the following fields:
<a href="http://teamtutorials.com/wp-content/uploads/2008/07/database-search-form-01.jpg"><img src="http://teamtutorials.com/wp-content/uploads/2008/07/database-search-form-01.jpg" alt="" title="database-search-form-01" width="500" height="103" class="alignnone size-full wp-image-1602" /></a>]]></description>
			<content:encoded><![CDATA[<p>In this tutorial I will show you how to add a simple search box to query a MySQL database. This tutorial is a continuation of the <a href="http://teamtutorials.com/web-development-tutorials/how-to-access-a-mysql-database-using-php">“How to Access a MySQL Database Using PHP”</a> tutorial. Since writing that original tutorial I have created a<a href="http://teamtutorials.com/web-development-tutorials/setting-up-a-wamp-server"> local WAMP test environment</a>. I have created the database “test” with a table “testable”. The table contains the following fields:<br />
<a href="http://teamtutorials.com/wp-content/uploads/2008/07/database-search-form-01.jpg"><img src="http://teamtutorials.com/wp-content/uploads/2008/07/database-search-form-01.jpg" alt="" title="database-search-form-01" width="500" height="103" class="alignnone size-full wp-image-1602" /></a></p>
<p>I have added some sample data to this table.<br />
<a href="http://teamtutorials.com/wp-content/uploads/2008/07/database-search-form-02.jpg"><img src="http://teamtutorials.com/wp-content/uploads/2008/07/database-search-form-02.jpg" alt="" title="searching a mysql database" width="364" height="176" class="alignnone size-full wp-image-1603" /></a></p>
<p>For our search to work we will have to create two files. One file will be the PHP script to search our form and the other will be an HTML page containing our form and passing the search variable to our PHP file.<br />
I will start by create an search.htm file. Below you will see the basic structure of our simple HTML page.</p>
<pre class="brush: xml; title: Code Snippet:; notranslate">
&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;Search the Database&lt;/title&gt;
	&lt;/head&gt;

	&lt;body&gt;

	&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Next we will add the form. Notice that the action=”search.php” and the method=”post”. This is basically telling the web server to send Post variables to the search.php page. Another important thing to note is the input box name field. This field, name=”term” , this will pass whatever is in the input box on as a post variable named “term”. We also add a submit button.</p>
<pre class="brush: xml; title: Code Snippet:; notranslate">
&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;Search the Database&lt;/title&gt;
	&lt;/head&gt;

	&lt;body&gt;

	&lt;form action=&quot;search.php&quot; method=&quot;post&quot;&gt;
  	 Search: &lt;input type=&quot;text&quot; name=&quot;term&quot; /&gt;&lt;br /&gt;
    &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
	&lt;/form&gt;

	&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Now on to search.php .  The first thing I did was to simple echo the post variable to make sure the information is getting passed from the search form.</p>
<pre class="brush: php; title: Code Snippet:; notranslate">

&lt;?php
      echo $_POST['term'];
?&gt;
</pre>
<p>If you variable is not being displayed then something is wrong. If your search term is displayed then you can move on. We can delete the echo code from our php file now.<br />
First make a connection to your database</p>
<pre class="brush: php; title: Code Snippet:; notranslate">

&lt;?php

mysql_connect (&quot;localhost&quot;, &quot;testuser&quot;,&quot;password&quot;)  or die (mysql_error());

?&gt;
</pre>
<p>Now select your test database</p>
<pre class="brush: php; title: Code Snippet:; notranslate">

&lt;?php

mysql_connect (&quot;localhost&quot;, &quot;testuser&quot;,&quot;password&quot;)  or die (mysql_error());
mysql_select_db (&quot;test&quot;);

?&gt;
</pre>
<p>Next we are going to store the post variable as $term and build our query. As you can see we are searching the first name field. We are searching for a first name like %$term%. The % character is a wild card for 0 or more charcters. So if we had ‘bob’ in our database and we entered the ‘bob’ in the search box it would return the results. With the % charcters around the term we could also search for ‘ob’ and it would return the results for bob, and any other name containing ‘ob’.</p>
<pre class="brush: php; title: Code Snippet:; notranslate">
&lt;?php
mysql_connect (&quot;localhost&quot;, &quot;testuser&quot;,&quot;password&quot;)  or die (mysql_error());
mysql_select_db (&quot;test&quot;);

$term = $_POST['term'];

$sql = mysql_query(&quot;select * from testtable where FName like '%$term%'&quot;);

?&gt;
</pre>
<p>The next step is to execute the query and display the results.</p>
<pre class="brush: php; title: Code Snippet:; notranslate">
&lt;?php
mysql_connect (&quot;localhost&quot;, &quot;testuser&quot;,&quot;password&quot;)  or die (mysql_error());
mysql_select_db (&quot;test&quot;);

$term = $_POST['term'];

$sql = mysql_query(&quot;select * from testtable where FName like '%$term%'&quot;);

while ($row = mysql_fetch_array($sql)){
	echo 'ID: '.$row['ID'];
	echo '&lt;br/&gt; First Name: '.$row['FName'];
	echo '&lt;br/&gt; Last Name: '.$row['LName'];
	echo '&lt;br/&gt; Phone: '.$row['Phone'];
	echo '&lt;br/&gt;&lt;br/&gt;';
	}

?&gt;
</pre>
<p>Now I will test it out. Since I know ‘Bob’ is in the database I will search for him.<br />
<a href="http://teamtutorials.com/wp-content/uploads/2008/07/database-search-form-03.jpg"><img src="http://teamtutorials.com/wp-content/uploads/2008/07/database-search-form-03.jpg" alt="" title="searching a mysql database" width="296" height="106" class="alignnone size-full wp-image-1604" /></a></p>
<p>If all goes well you should have these results returned/<br />
<a href="http://teamtutorials.com/wp-content/uploads/2008/07/database-search-form-04.jpg"><img src="http://teamtutorials.com/wp-content/uploads/2008/07/database-search-form-04.jpg" alt="" title="searching a mysql database" width="178" height="108" class="alignnone size-medium wp-image-1605" /></a></p>
<p>So we can search for First names only. If you want to give the user the ability to enter either a first name or a last name change your query to this:</p>
<pre class="brush: php; title: Code Snippet:; notranslate">
$sql = mysql_query(&quot;select * from testtable where FName like '%$term%' or LName like '%$term%' &quot;);
</pre>
<p>To test that query I did a simple search for the letter ‘o’. Which should return several first names and last names that contain the letter.<br />
<a href="http://teamtutorials.com/wp-content/uploads/2008/07/database-search-form-05.jpg"><img src="http://teamtutorials.com/wp-content/uploads/2008/07/database-search-form-05.jpg" alt="" title="searching a mysql database" width="172" height="406" class="alignnone size-medium wp-image-1606" /></a></p>
<p>So there you have it a simple basic database search form. I hope this all makes sense, I am writing this at 4:00am and am a bit tired. As always feel free to ask questions.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://teamtutorials.com/web-development-tutorials/editing-mysql-data-using-php" title="Editing MySQL Data Using PHP">Editing MySQL Data Using PHP</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/access-a-mysql-database-using-pdo" title="Access a MySQL Database Using PDO">Access a MySQL Database Using PDO</a></li><li><a href="http://teamtutorials.com/windows-tutorials/downloading-and-installing-heidi-sql" title="Downloading and Installing Heidi SQL">Downloading and Installing Heidi SQL</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/sending-e-mail-to-validate-user-sign-up" title="Sending E-Mail to validate User Sign-up">Sending E-Mail to validate User Sign-up</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/how-to-return-mysql-results-to-a-table" title="How to Return MySQL Results to a Table">How to Return MySQL Results to a Table</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/web-development-tutorials/php-tutorials/creating-a-form-that-will-search-a-mysql-database/feed</wfw:commentRss>
		<slash:comments>135</slash:comments>
		</item>
	</channel>
</rss>

