Re: Sql/PHP question

From: Date: Wed, 16 Apr 2003 18:51:29 +0000
Subject: Re: Sql/PHP question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-144175@lists.php.net to get a copy of this message
Here is what I had to do to make it so that "where" and "and" appeared in the right places at the right times. $department_query = ""; if ($department != "all") {
    $department_query="department='$department'";
} $location_query = ""; if ($location != "all") {
    $location_query="location='$location'";
} $where = "where"; if ($location == "all" && $department == "all") { $where = ""; } $and = ""; if ($department != "all") { $and = "and"; } $query = "select * from $database_table $where $department_query $and $location_query"; On Wednesday, April 16, 2003, at 01:21 PM, CPT John W. Holmes wrote:
I am creating a page that will generate a report. On the first page I have drop down menus that you can select what you want in the report. The table, department, and location. So in the query, I have this: $query = "select * from $database_table where department = $department and location = $location"; Now this works fine if in the department menu I select a department, and in the location menu I select a location, but I also want to be able to show all. For instance I want to create a report on all items for a single department in all locations. Is there such a thing as a wildcard within a where statement? location = * doesn't work of course, but what else could I use.
Remember, queries are just strings. Create the right string to suit your needs. If $location is set, then add it to your where clause, if $department is set, add it, too. if(isset($location)) { $where[] = " location = $location "; } if(isset($department)) { $where[] = " department = $department "; } $query = "select * from $database_table where " . implode(' and ', $where); You may want to use empty() instead of isset(), or check for a certain value, that's up to you. Are you sure you don't need quotes around $department and $location in your query? Are those integer or string columns? ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#144175) next »