PDA

View Full Version : mysql_query a mysql_query???


lpeek
06-21-2007, 11:56 AM
Hey,

ok so im setting up a 'browse' feature on my website (an archive of bands) and i want to search through what the user selects. But the user can select multiple things which would be difficult to group into one mysql_query.

is there a way i can do another query, on the results of a query ive already done??

something like this:


$results = mysql_query("SELECT * FROM `bands` WHERE `genre` = 'Rock'") or die(mysql_error());

$results_refined = mysql_query("SELECT * FROM $results WHERE `name` LIKE %'$name'%);


is that possible?

or do i have to do something else?

thanks.

rafiki
06-21-2007, 01:10 PM
erm $results is not a table, so yes you would have to do something else
try
$results = mysql_query("SELECT * FROM `bands` WHERE `genre` = 'Rock' && `name` LIKE %'$name'%);
P.S its good to be back :)

lpeek
06-21-2007, 01:31 PM
Yeah, i did think of that originally... but i have a lot of things to search. i have 4 different mysql_query's just to search different options on the name of an artist... so to incorporate the other search options into that as well i would have to do a mysql_query for every single combination of searches...

it would be quite silly and im sure there must be a quicker way... here is what i have so far:


// DISPLAY SEARCH RESULTS

if ($cmd=="search") {

//NAME SEARCH

if (!empty($name)) {

// next block to search 'exact' name, 'start' of name, 'end' of name and 'any part' of name

if ($wholepart=="exact"){
$bands = mysql_query ("SELECT * FROM `bands` WHERE `name`='".$name."' ORDER BY `name` ASC") or die(mysql_error());
} else if($wholepart=="start") {
$bands = mysql_query ("SELECT * FROM `bands` WHERE `name` LIKE '".$name."%' ORDER BY `name` ASC") or die(mysql_error());
} else if($wholepart=="end") {
$bands = mysql_query ("SELECT * FROM `bands` WHERE `name` LIKE '%".$name."' ORDER BY `name` ASC") or die(mysql_error());
} else if($wholepart=="part") {
$bands = mysql_query ("SELECT * FROM `bands` WHERE `name` LIKE '%".$name."%' ORDER BY `name` ASC") or die(mysql_error());
}

} else {

//NO SEARCH ENTERED AT ALL
$bands = mysql_query ("SELECT * FROM `bands` ORDER BY `name` ASC") or die(mysql_error());

}

//SECOND LOT OF SEARCH OPTIONS MUST GO HERE

// GENRE 1
// GENRE 2

} else {

$bands = mysql_query ("SELECT * FROM `bands` ORDER BY `name` ASC") or die(mysql_error());

}

$numbands = mysql_num_rows($bands);