SQL search - Distiguishing between search criteria using Drop-down form/menu

Hi guys/gals,

I am currently attempting to distinguish between different search options using a drop-down menu on my website.

The basic funtion of the website is a Music samples search engine. The different drop down options include Artist, Song, Album etc…

Basically, the SQL query should be identical on each query, which makes things alot easier as the output should be the same for each query, depending on the search option.

Here is the form for the search bar, search option list and “Find Sample” button
[hr]

  <label>
    <select name='selection' form action='search.php' method='GET'>
      <option value="1">Sampling Artist</option>
      <option value="2">Sampling Song</option>
      <option value="3">Artist</option>
      <option value="4">Song</option>
      <option value="5">Sampling Album</option>
      <option value="6">Album</option>
    </select>
  </label>
 <input type='submit' name='submit' value='Find Samples'>

</center>

[hr]

Here is the SQL query for all the different search options…

[hr]
$button = $_GET[‘submit’];
$search = $_GET[‘search’];
$selection = $_GET[‘selection’];

if (!$button){
echo ‘you didnt submit a keyword’;
}
elseif (strlen($search)<3){
echo ‘search length too short!’;
}

else 
echo "you searched for <b>$search</b>";	

	mysql_connect("localhost","root","");		mysql_select_db("samples");

	$sql="SELECT sampling_artist_name, sampling_song_name, song_name, artist_name, sampling_album_name, sampling_album_year
	FROM sampling_artists, sampling_songs, songs, sample_refs, artists, sampling_albums
	WHERE sampling_artists.sampling_artist_id = sampling_songs.sampling_artist_id 
	AND sampling_songs.sampling_song_id = sample_refs.sampling_song_id
	AND songs.song_id = sample_refs.song_id
	AND sampling_albums.sampling_album_id = sampling_songs.sampling_album_id
	AND artists.id = songs.artist_id
	AND sampling_artist_name LIKE '%".$search."%'
	ORDER BY artist_name asc";

[hr]

I was wondering if i could do something such as this…
[hr]
if $selection = [‘option=“1”’];
AND sampling_artist_name LIKE ‘%".$search."%’

else if $selection = [‘option=“2”’];
AND sampling_song_name LIKE ‘%".$search."%’

[hr]

In other words, would simply adding an “if” statement somewhere withing the SQL Query be enough to distinguish between search options?

Many thanks.

Yes, you may do if/else to come up with different search options as specified by the user…

And if you don’t want to do this, you can also accomplish it by issuing SQL query something like this:
[php]
AND sampling_artist_name LIKE ‘%".
$search."%’ OR sampling_song_name LIKE ‘%".$search."%’ OR sampling_album_name LIKE ‘%".$search."%’…
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service