Good Morning PHP-DB Listers,

I have a PHP GUI in which a select box gets populated by a query of a 
MySQL DB for all CD's by all artists when the page is first opened. I am 
now trying to implement 
the same query but with a WHERE clause that filters the returned CD's to 
be displayed in the same select box according to the artist selected via a 
group of checkboxes (one per artist).  The problem so far is that if I try 
to implement some flow control (if checbox one is selected, append "where 
artist='hendrix' to the select all CDs query, mysql_query() etc...), I get 
an error message saying that the query string is empty. So I am looking 
for some advice about how to implement the logic of having the same select 
box display the results of different but related queries.

For example, 

$query_1 = "SELECT CD_title FROM CDs order by CD_title";
$result_1 = mysql_query($query_1) or die("Can't select CD_title from CDs" 
. mysql_error());
while($row=mysql_fetch_array($result_1, MYSQL_BOTH)) ..etc...

works just fine.

But if I try to use some flow control to alter which query statement is 
assigned to $query_1...

if(checkbox one is selected)
{
        $query_1 = "SELECT CD_title FROM CDs WHERE artist_name = 'Hendrix' 
order by CD_title";
}

elseif(no checkboxes are checked)
{ 
        $query_1 = "SELECT CD_title FROM CDs order by CD_title";
}

$result_1 = mysql_query($query_1) or die("Can't select CD_title from CDs" 
. mysql_error());
while($row=mysql_fetch_array($result_1, MYSQL_BOTH)) ..etc...

produces the error message stated above ... that the query string is 
empty. I've also tried to duplicate the "mysql_query($query_1) or 
die()..." within each section of the IF group, but then the select box 
does not get populated at all.

I am using session variables, and I'm certain that the checkboxes are 
correctly assigning the necessary values to the variables associated with 
them because I am echo'ing the variables and I can see the values that 
they have.

And when I run the query with the where clause in the MySQL client, it 
works just fine.

I'm guessing I'm missing something fairly simple because this type of 
functionality must have been implemented before.

Thanks for reading the rather long question. 

David

Reply via email to