Searching a table MySQL & PHP.
Posted
by S1syphus
on Stack Overflow
See other posts from Stack Overflow
or by S1syphus
Published on 2010-05-17T13:03:57Z
Indexed on
2010/05/17
13:10 UTC
Read the original article
Hit count: 296
I want to be able to search through a MySQL table using values from a search string, from the url and display the results as an XML output.
I think I have got the formatting and declaring the variables from the search string down.
The issue I have is searching the entire table, I've looked over SO for previous answers, and they all seem to have to declare each column in the table to search through.
So for example my database layout is as follows:
**filesindex**
-filename
-creation
-length
-wall
-playlocation
First of all would the following be appropriate:
$query = "SELECT * FROM filesindex WHERE filename LIKE '".$searchterm."%'
UNION
SELECT * FROM filesindex WHERE creation LIKE '".$searchterm."%'
UNION
SELECT * FROM filesindex WHERE length LIKE '".$searchterm."%'
UNION
SELECT * FROM filesindex WHERE wall LIKE '".$searchterm."%'
UNION
SELECT * FROM filesindex WHERE location LIKE '".$searchterm."%'";
Or ideally, is there an easier way that involves less hardcoding to search a table.
Any ideas?
Thanks
© Stack Overflow or respective owner