Web Hosting Talk







View Full Version : [PHP] SQL Query


djwayne_2004
01-05-2005, 08:52 AM
Hi everyone,

I have just built a search engine for a customer using php and mysql. It was all working fine until i am told by the customer that they required it to search differently which i am not sure on how to do.

Basically the search engine finds computer games cheats against a mysql database, at this moment it time the query looks something like this:

SELECT * FROM games WHERE game_title LIKE '%" . $q . "%'

Which will return the results the user is searching for but the customer is asking that if $q was a single charater, e.g "A". They require that cheats only by the letter "A" would be returned.

I hope someone here would be able to give me some advice on how to fix this problem.

Thanks
Wayne

error404
01-05-2005, 09:24 AM
You'd probably want to just use some simple PHP logic to determine if it's a single character request. Then just remove (or never add) the first %.

k2host
01-05-2005, 05:55 PM
pretty much what error404 said: do something like:


if (strlen($q) > 1) {
mysql_query("SELECT * FROM games WHERE game_title LIKE '%" . $q . "%'");
} else {
mysql_query("SELECT * FROM games WHERE game_title LIKE '" . $q . "%'");
}

djwayne_2004
01-05-2005, 05:58 PM
Thankyou everyone i knew it was something simple, i have just never done it like that before. Search page is all up and running now thanks to you guys and girls!

Wayne