Page 1 of 1

Newbie trying to code a mysql search

Posted: Mon Apr 05, 2010 2:30 pm
by steven.cottom
I am new to php and have created a simple HTML form that inserts data into a mysql database.

I am trying to create a search page so that the user can type in a string that will search the surname column and return all information in the row.

Here is my code:

Code: Select all

<?php 
mysql_connect ("localhost", "myuser", "mypassword") or die (mysql_error());
mysql_select_db ("enquiries");

$search = $_POST['search'];
$sql = mysql_query("select * FROM enquiry WHERE surname LIKE '%search%'");

while ($row = mysql_fetch_array( $sql ))
{	
	echo 'Firstname: '.$row['firstname'];
	echo '<br/>Surname: '.$row['surname'];
	echo '<br/>Tutor: '.$row['tutor'];
}
?>
When i click the submit button on my search.html page there are no errors displayed but nothing is echo'd out either. Just an empty page is displayed.

Any help would be appreciated.

Re: Newbie trying to code a mysql search

Posted: Mon Apr 05, 2010 2:40 pm
by mikosiko

Code: Select all

$sql = mysql_query("select * FROM enquiry WHERE surname LIKE '%search%'");
you are not using your variable $search after LIKE

Re: Newbie trying to code a mysql search

Posted: Mon Apr 05, 2010 2:47 pm
by steven.cottom
sorry i dont understand what you mean. can you explain in a bit more detail. :mrgreen:

Thanks for replying

Re: Newbie trying to code a mysql search

Posted: Mon Apr 05, 2010 2:53 pm
by mikosiko
ok... here is part of your code

Code: Select all

$search = $_POST['search'];
$sql = mysql_query("select * FROM enquiry WHERE surname LIKE '%search%'");

as you will see... you assigned the value of $_POST['search'] to the variable $search
but in your select you are NOT using that variable ... that is why your select fail

Re: Newbie trying to code a mysql search

Posted: Mon Apr 05, 2010 3:03 pm
by steven.cottom
Aaaah! i understand now.

Thanks for your help!