0

I have been asked if I can build a search for when the user doesn't know their full name or exact spelling of their name.

This is part of a larger search. The section where I just search for staff with their surname and forename works. Most of the resolutions are using different syntax to mine and as I'm relatively new to coding I'm finding it difficult to read and understand.

Here is my code:

}elseif ($name == '' && $surname == '' && $part !== ''){
$firstName = $part;
$surName = $part;

$query='SELECTt1.staffNum,t1.firstName,t1.surname,t1.jobTitle,t1.hospSite,t1.dept,t1.phone,t1.email,t1.assigned,t1.reason,t1.accessLevel,t1.leaver,t3.course_date,t3.trainingModules
FROM staff_tbl t1 
INNER JOIN evaluation_tbl t2
ON t1.staffNum=t2.staffNum
INNER JOIN trainingcourse_tbl t3
ON t2.course_id = t3.course_id 
WHERE t1.firstName LIKE : firstName
OR t1.surname LIKE : surName
ORDER BY t1.staffNum';
$firstName = '%$firstName%';
$surName = '%$surName%';
$myStatement= $mydb->prepare($query);
$myStatement->bindValue(':firstName',$firstName);
$myStatement->bindValue(':surName',$surName);
$myStatement->execute();
$results = $myStatement->fetchAll();
$myStatement->closeCursor();
Return $results;

I am entering 'ary' on the front end and should get any staff with that in their name ie Mary Bury but I just get 'no results found'. If I run this query modified in phpMyAdmin SQL it works

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Jo99
  • 1
  • 1
  • sorry the formatting seems to have gone wrong – Jo99 May 31 '19 at 10:05
  • I dont think, that `$firstName = '%$firstName%';`is right.. shouldn't it be more like `$firstName = '%' . $firstName . '%'` ? You made a String with `%$firstName%`in it, ofc, it cant find anything because I dont think any of your names in the DB hava a `$` inside them – DigitalJedi May 31 '19 at 10:49
  • Thanks so much, it works now. Very much appreciated – Jo99 May 31 '19 at 11:25

0 Answers0