Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial



Hi guys,how do I link my search form such that I can get info from my database once I type my name in the search form using php and mysql.Here is a copy of my php/hml; <!DOCtype html> <htmtl> <head> <title>Student details </title> </head> <body> <form action="search.php?go" method="post" id="searchform"> <select>


</select> <input type="text" name="search" value="Type search" required/> <input type="submit" name="submit" value="submit" required/> </form> <?php if(isset($_POST['submit'])){ if(isset($_GET['go'])){ $search=$_POST['search'];

//connect to database $db=msql_connect("localhost","root",""); $mydb=msql_select_db("school")or die("can't connect"); $sql="SELECT Name,ID,Gender from student"; $result=msql_query($sql); while($row=msql_fetch_array($result)){ $Name=$row['Name']; $ID=$row['ID']; $Gender=$row['Gender']; //-display the result of the array echo "<ul>\n"; echo "<li>" . "<a href=\"search.php?id=$ID\">" .$Name . $Gender . "</a></li>\n"; echo "</ul>"; }

else{ echo "Please enter a search query"; } } } ?> </body> </html>

Hi Tom, This was a basic search trial and I am really greatful for your assistance.The problem is that when I run the code,I get a blank page.Have you seen the code that I sent?

Hi Tom, Here is the code once again; <!DOCTYPE html> <htmtl> <head> <title>Student details </title> </head> <body> <form action="index.php" method="post"> <select>


</select> <input type="text" name="query" value="Type search" required/> <input type="submit" name="submit" value="submit" required/>

</form> <?php //connect to database mysql_connect("localhost","root","") or die("can't connect"); mysql_select_db("school");


if(isset($_POST['query'])){ $query1=$_POST['query'];

$sql=("SELECT*FROM student WHERE Name,ID,Gender`LIKE '%'.$Name.'%'") or die(mysql_error()); $result=mysql_query($sql); while($row=mysql_fetch_array($result)){ $Name=$row['Name']; $ID=$row['ID']; $Gender=$row['Gender']; //-display the result of the array //echo "<ul>\n"; // echo "<li>" . "<a href=\"search.php?id=$ID\">" .$Name . $Gender . "</a></li>\n"; //echo "</ul>"; } }

else {echo"no"; }


?> </body> </html>

Hi guys, What would make me get this warning?What should I do to it to clear it?

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\search\index.php on line 13 Collins

1 Answer

Try "Enhancing a Simple PHP Application" http://teamtreehouse.com/library/enhancing-a-simple-php-application-2.

There's two badges for Adding Search - these should definitely answer your question.

You'll have to use a WHERE and LIKE statement in your MYSQL command. I'm sure you're already familiar with the WHERE command, so here's the documentation for LIKE.


Also, I would suggest using PDO to interface with the database (Php Data Object, also used in the two mentioned badges). I think you're current method is subject to mysql injection - someone entering a search query in the form could add naughty mysql commands and cause great issues with your database i.e. deleting tables. Binding parameters via PDO is one of the most effective ways of stopping injection (I believe).