Forum Moderators: open

Message Too Old, No Replies

Query Help!

With PHP

         

Jealy

2:08 am on Feb 13, 2009 (gmt 0)

10+ Year Member



I'm doing a simple index website for films and I would like to be able to search my database and it would display the results. (by adding "?search=" to my url)

So far I have it so that it will return by first letter;

$letter=$_GET['search'];

SELECT Name FROM films WHERE Name LIKE '$letter%'

I figured that if I did the same without the % it would select any part of the Name and return them.

SELECT Name FROM films WHERE Name LIKE '$letter'

I tried it and it didn't work.

Anyone have any ideas?

Thanks

J

LifeinAsia

4:47 pm on Feb 13, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You'll need wildcards on both sides of the input string:
SELECT Name
FROM films
WHERE Name LIKE '%$letter%'

Of course, if someone enters "a" you will get every single movie that has an "a" anywhere in the title. So you may want to require a minimum number of letters to be in the input string.

Oh, and of course you'll also want to use the standard procedures to prevent SQL injection attacks.

[edited by: LifeinAsia at 4:49 pm (utc) on Feb. 13, 2009]

Jealy

2:10 pm on Feb 14, 2009 (gmt 0)

10+ Year Member



Another wildcard!

Worked a treat! Can't believe I didn't think of that! Thanks!

J

[edited by: Jealy at 2:10 pm (utc) on Feb. 14, 2009]