|
Posted by Captain Paralytic on 11/08/07 15:12
On 8 Nov, 14:13, jc...@lycos.com wrote:
> $query = "SELECT DISTINCT names
> FROM $table
> Order by names";
> $result = mysql_query($query);
>
> In the code above, 'Order by' works very well when not used in
> conjunction with other query-able items but how about when used in a
> query where other possible choices are included? How do I use 'Order
> by' in the query below where it will only act on 'names' when "All"
> is
> selected and otherwise, not really be active during the query as in
> the code below? I'm using PHP to access MySQL. Thanks
>
> $query = "SELECT *
> FROM $table
> WHERE 1 = 1 ";
> if($year != "All") $query .= "and year = '".$year."'";
> if($status != "All") $query .= "and status = '".
> $status."'";
> if($names != "All") $query .= "and names = '".$names."'";
> $result = mysql_query($query);
ORDER BY names
the whole WHERE clause is superfluous in this query.
Navigation:
[Reply to this message]
|