Jump to content

WHERE field contains....a challenge!


pablo1988

Recommended Posts

Hi,

 

My code below only allows me to search exact terms and therefore does not allow for spelling mistakes of names etc. Please can somebody advise how I can alter the code below to allow a user to search part of the search criteria and still retrieve results e.g. a search for "business" would return "business analysis", "business architecture" and so on. Any help would be greatly appreciated.

 

$sql="SELECT DISTINCT First_Name, Last_Name, l.Resource_ID FROM ((resource l inner join resource_skill ln on l.Resource_ID = ln.Resource_ID) inner join skill n on ln.Skill_ID = n.Skill_ID) WHERE First_Name LIKE '$fname' OR Last_Name LIKE '$lname' OR Skill_Name LIKE '$skill'";

 

Thanks a lot!

 

Paul

Link to comment
Share on other sites

SELECT * FROM `table` WHERE `column` LIKE '%{$value}%';

 

% is a wildcard. In this instance it means search for a term with anything either side of it. If you were to put '{$value}%' it would only allow for additional characters after the term and vice-versa.

Link to comment
Share on other sites

Thanks CPD, I have applied % to both sides of skill only, however when I search by name and leave skill blank it brings back all records. I assume because I have used % on both sides it takes anything??

 

$sql="SELECT DISTINCT First_Name, Last_Name, l.Resource_ID FROM ((resource l inner join resource_skill ln on l.Resource_ID = ln.Resource_ID) inner join skill n on ln.Skill_ID = n.Skill_ID) WHERE First_Name LIKE '{$fname}' OR Last_Name LIKE '{$lname}' OR Skill_Name LIKE '%{$skill}%'";

 

See above code.

 

Please can you advise what I should do to fix this?

 

Thanks.

Link to comment
Share on other sites

a little something like this:

 if (!empty($skill)) {
$skillQry = " OR Skill_Name LIKE '%$skill%'";
}
else{
$skillQry = '';
}

$sql="SELECT DISTINCT First_Name, Last_Name, l.Resource_ID FROM ((resource l inner join resource_skill ln on l.Resource_ID = ln.Resource_ID) inner join skill n on ln.Skill_ID = n.Skill_ID) WHERE First_Name LIKE '{$fname}' OR Last_Name LIKE '{$lname}' $skillQry";

Link to comment
Share on other sites

Muddy_Funster would you do me a massive favor and add the code into the below please? I don't want to mess it up. It would be hugely appreciated!

 

<?php

 

if(isset($_POST['submit'])){

 

 

$fname = $_POST['fname'];

$lname = $_POST['lname'];

$skill = $_POST['skill'];

   

//connect  to the database

$db=mysql_connect  ("127.0.0.1", "root",  "") or die ('I cannot connect to the database  because: ' . mysql_error());

   

//select  the database to use

$mydb=mysql_select_db("resource matrix");

   

 

//query  the database table

$sql="SELECT DISTINCT First_Name, Last_Name, l.Resource_ID FROM ((resource l inner join resource_skill ln on l.Resource_ID = ln.Resource_ID) inner join skill n on ln.Skill_ID = n.Skill_ID) WHERE First_Name LIKE '{$fname}' OR Last_Name LIKE '{$lname}' OR Skill_Name LIKE '%{$skill}%'";

 

Link to comment
Share on other sites

Not much to mess up really, but here ya go :

<?php

if(isset($_POST['submit'])){


$fname = mysql_real_escape_string($_POST['fname']);   // these changes
$lname = mysql_real_escape_string($_POST['lname']);   // are important
$skill = mysql_real_escape_string($_POST['skill']);   // please find out why!
   
//connect  to the database
$db=mysql_connect  ("127.0.0.1", "root",  "") or die ('I cannot connect to the database  because: ' . mysql_error());
   
//select  the database to use
$mydb=mysql_select_db("resource matrix");

//build condition for WHERE:
if (!empty($skill)) {
$skillQry = " OR Skill_Name LIKE '%$skill%'";
}
else{
$skillQry = '';
}   

//query  the database table
$sql="SELECT DISTINCT First_Name, Last_Name, l.Resource_ID FROM ((resource l inner join resource_skill ln on l.Resource_ID = ln.Resource_ID) inner join skill n on ln.Skill_ID = n.Skill_ID) WHERE First_Name LIKE '{$fname}' OR Last_Name LIKE '{$lname}' $skillQry";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.