Why is my LIKE Search not working?

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
EverToDesign
Forum Newbie
Posts: 10
Joined: Sat Oct 11, 2003 9:38 am

Why is my LIKE Search not working?

Post by EverToDesign »

Hey there, basically i've got several IF statements to add a WHERE or AND clause along with title = '$keyword' as below:

$sql .= " WHERE title LIKE '$keyword'";
echo "test4";

Test4 IS echoed out so i know the script isn't dying on me. Test echoes are also printed before and after the while loop that fetches the results, the only one which isn't echoed out is the one within the While loop that pulls out search results.

At current my keyword im trying to use is test which is searched for in the title field.

the following are in the titles column of the mySQL Database
Test Title
Test Job Title22
This is a testMod
TestMod

Yet none of them show up at all. Am I doing the wrong sort of query here? I just want a similarity style search not an exact one i thought LIKE would pull all those out as they have test in them?
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post by Jean-Yves »

The LIKE keyword usually needs a wildcard character (can't remember off the top of my head which one MySQL uses, probably % or *)

So, for example:

SELECT * FROM staff WHERE surname LIKE "SMITH%"

Should get SMITH, SMITHSON, SMITHEY, etc

If you put a wildcard at the start too, "%SMITH%", you'd also get anything with SMITH in it, not just starting with SMITH, eg ASMITH, BSMITHSON, etc (sorry, couldn't think of a proper surname)
Post Reply