Only geting exact match from database query

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
seriousdamage
Forum Commoner
Posts: 30
Joined: Sat Nov 27, 2004 10:18 am

Only geting exact match from database query

Post by seriousdamage »

Hi,
when I run the following query I only get the exact match of what I have searched for,
I think I need to use % sign so that I can search for Nic and get also Nicola as a result.
But I can figuer out where to add the %

I use an external webform for the search.

Code: Select all

$result = mysql_query("SELECT * FROM contacts where first_name LIKE '" . $_POST['search'] ."' OR last_name LIKE '" . $_POST['search'] ."' OR company_name LIKE '" . $_POST['search'] ."' OR website LIKE '" . $_POST['search'] ."' OR street LIKE '" . $_POST['search'] ."' OR city LIKE '" . $_POST['search'] ."' OR zip LIKE '" . $_POST['search'] ."' OR country LIKE '" . $_POST['search'] ."' OR mail1 LIKE '" . $_POST['search'] ."' OR mail2 LIKE '" . $_POST['search'] ."' OR phone LIKE '" . $_POST['search'] ."' OR mobile LIKE '" . $_POST['search'] ."' OR card LIKE '" . $_POST['search'] ."' OR birthday LIKE '" . $_POST['search'] ."'") or die (mysql_errno().": ".mysql_error()."<BR>");
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: Only geting exact match from database query

Post by SidewinderX »

Your query is an SQL injection waiting to happen. Anyway, you are right, you need to use the wildcard %

Code: Select all

$search = mysql_real_escape_string($_POST['search']);
$result = mysql_query("SELECT * FROM `contacts` WHERE `first_name` LIKE '%{$search}%' ....);
seriousdamage
Forum Commoner
Posts: 30
Joined: Sat Nov 27, 2004 10:18 am

Re: Only geting exact match from database query

Post by seriousdamage »

cool, works great, thanks a lot.
Post Reply