Newbie trying to code a mysql search

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
steven.cottom
Forum Newbie
Posts: 10
Joined: Mon Apr 05, 2010 2:14 pm

Newbie trying to code a mysql search

Post 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.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Newbie trying to code a mysql search

Post by mikosiko »

Code: Select all

$sql = mysql_query("select * FROM enquiry WHERE surname LIKE '%search%'");
you are not using your variable $search after LIKE
steven.cottom
Forum Newbie
Posts: 10
Joined: Mon Apr 05, 2010 2:14 pm

Re: Newbie trying to code a mysql search

Post by steven.cottom »

sorry i dont understand what you mean. can you explain in a bit more detail. :mrgreen:

Thanks for replying
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Newbie trying to code a mysql search

Post 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
steven.cottom
Forum Newbie
Posts: 10
Joined: Mon Apr 05, 2010 2:14 pm

Re: Newbie trying to code a mysql search

Post by steven.cottom »

Aaaah! i understand now.

Thanks for your help!
Post Reply