Multiple WHEREs

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Multiple WHEREs

Post by AliasBDI »

I was hoping to have a query that would check for a WHERE = <URLvariable> and if nothing, then go to SETVARIABLE.

Here is my code:

Code: Select all

SELECT * FROM con_company WHERE active = '%s' ORDER BY company ASC
The variable will be either a 'Y' or 'N'. I want it to check for either, and if no URLvariable is passed, then use 'Y'. Any ideas?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Here's some pseudocode that might help you plan things out:

Code: Select all

if ($urlvariable) {
  // urlvariable is set
  $where_val = $urlvariable;
} else {
  // urlvariable not set, use default value (Y)
  $where_val = 'Y';
}

$query_text = "SELECT * FROM con_company WHERE active = '$where_val' ORDER BY company ASC";

?>
Hope it helps
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$sql = sprintf('SELECT * FROM con_company WHERE active = ''%s'' ORDER BY company ASC', (isset($_GET['URLvariable']) && ($_GET['URLvariable'] == 'Y' || $_GET['URLvariable'] == 'N')) ? $_GET['URLvariable'] : 'Y');
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Not sure I'm doing it right, here is my code.... without any of your suggestions.

Code: Select all

$query_queryLIST = sprintf("SELECT * FROM con_company WHERE active = '%s' ORDER BY company ASC", $colname_queryLIST);
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Out of curiousity why are you using sprintf() ?
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

because

Post by AliasBDI »

I'm using Macromedia DreamWeaver MX 2004. That is what it creates.
Serberus
Forum Newbie
Posts: 5
Joined: Fri Aug 13, 2004 6:31 pm
Location: Hertfordshire, UK

Post by Serberus »

$query_queryLIST = 'SELECT *, IF (active = "Y" || ISNULL(active), "Y", "N") as activity FROM con_company ORDER BY activity, company ASC';

I can't test it because I don't have a similar table setup, does that work for ya?
Post Reply