# Selecting all (\*) within WHERE

**URL:** <https://forum.kirupa.com/t/selecting-all-within-where/260630>\
**Category:** programming\
**Created:** [May 17, 2008, 8:07pm UTC](https://forum.kirupa.com/t/selecting-all-within-where/260630 "2008-05-17T20:07:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![nburlington](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/nburlington/32/1365_2.png) [@nburlington](https://forum.kirupa.com/u/nburlington)\
**Post date:** [May 17, 2008, 8:07pm UTC](https://forum.kirupa.com/t/selecting-all-within-where/260630/1 "2008-05-17T20:07:20Z")

</div>

I have a .php file with the following query:

```auto
$query = "SELECT '$q' FROM Votes WHERE (AGE='$age' AND RACE='$race' AND GENDER='$gender')";

```

I’m feeding these variables in from Flash. This option doesn’t allow the user to select all from the Age, Race or Gender categories.

I tried:

```auto

$age = $_POST["age"];

// 0 = all ages;

if($age == "0"){
$age = "*";
}

```

But that doesn’t work when I feed it into the query.

Is there a way to have an option to select all within the WHERE method?

---

<div class="post-metadata">

**Author:** ![kdd](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kdd](https://forum.kirupa.com/u/kdd)\
**Post date:** [May 17, 2008, 8:49pm UTC](https://forum.kirupa.com/t/selecting-all-within-where/260630/2 "2008-05-17T20:49:05Z")

</div>

You can use OR, like select … from … where xyz=‘something’ OR abc=‘something else’;

Another way is to make a custom query depending on the input.  
Like,

```php

$query = 'SELECT * FROM TBL WHERE ';
if ( $age )
{
$query .= "AGE='$age'";
}
//and same with other 2, just make sure you don't end up having a query like: "SELECT * FROM TBL WHERE"
//because WHERE will expect something, so you'll get errors.

```

I hope I understood your problem correctly. 🙂

---

<div class="post-metadata">

**Author:** ![nburlington](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/nburlington/32/1365_2.png) [@nburlington](https://forum.kirupa.com/u/nburlington)\
**Post date:** [May 17, 2008, 11:46pm UTC](https://forum.kirupa.com/t/selecting-all-within-where/260630/3 "2008-05-17T23:46:15Z")

</div>

That’s great.

But what about the AND.

I’m worried that if I do:

if($age != “0”){  
$query .= “AGE=’$age’ AND”;  
}

if($gender != “0”){  
$query .= “GENDER=’$gender’”;  
}

If gender is 0 then I’m going to have a hanging AND. Will that break it?

---

<div class="post-metadata">

**Author:** ![kdd](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kdd](https://forum.kirupa.com/u/kdd)\
**Post date:** [May 18, 2008, 12:15am UTC](https://forum.kirupa.com/t/selecting-all-within-where/260630/4 "2008-05-18T00:15:09Z")

</div>

Yes, it’ll break it, so you have to check that also using if. 🙂

---

<div class="post-metadata">

**Author:** ![nburlington](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/nburlington/32/1365_2.png) [@nburlington](https://forum.kirupa.com/u/nburlington)\
**Post date:** [May 18, 2008, 2:36am UTC](https://forum.kirupa.com/t/selecting-all-within-where/260630/5 "2008-05-18T02:36:33Z")

</div>

Thanks! You really helped me get on my feet here. I appreciate it.

---

<div class="post-metadata">

**Author:** ![kdd](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kdd](https://forum.kirupa.com/u/kdd)\
**Post date:** [May 18, 2008, 4:30am UTC](https://forum.kirupa.com/t/selecting-all-within-where/260630/6 "2008-05-18T04:30:36Z")

</div>

np :pleased:

---

<div class="post-metadata">

**Author:** ![icio](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/icio/32/484_2.png) [@icio](https://forum.kirupa.com/u/icio)\
**Post date:** [May 18, 2008, 12:39pm UTC](https://forum.kirupa.com/t/selecting-all-within-where/260630/7 "2008-05-18T12:39:13Z")

</div>

I’d be tempted to implode an array of conditions.

```php
$query = "... WHERE (";
$conditions = [];

if ($age != '0') {
    $conditions[] = "AGE = '$age'";
}
if ($gender) {
    ...
}
...

$query .= implode(' AND ', $conditions).');';
echo $query;

```

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [May 18, 2008, 3:05pm UTC](https://forum.kirupa.com/t/selecting-all-within-where/260630/8 "2008-05-18T15:05:32Z")

</div>

```php

//assumes you've checked your input already
extract($_POST); //takes the $_POST array and creates separate vars using key names

$sql = 'SELECT * FROM table WHERE (';

if($age != '') {$ageCond = " age='$age'"; } else { $ageCond = '';}
if($race != '') {$raceCond = " race='$race'"; } else { $raceCond = '';}
if($gender != '') {$genderCond = " gender='$gender'";} else { $genderCond = '';}

if($ageCond != '') { $sql .= $ageCond; }
if(($raceCond != '' || $genderCond != '') && $ageCond != '') {$sql .= ' AND'; }
if($raceCond != '') {$sql .= $raceCond;}
if($raceCond != '' && $genderCond != '') { $sql .= ' AND'; }
if($genderCond != '') {$sql .= $genderCond;}

$sql .= ')';

```

That might work. There is undoubtedly an easier way, but this this is the first thing that comes to mind. Please let me know how it works out.

---

<div class="post-metadata">

**Author:** ![icio](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/icio/32/484_2.png) [@icio](https://forum.kirupa.com/u/icio)\
**Post date:** [May 18, 2008, 3:09pm UTC](https://forum.kirupa.com/t/selecting-all-within-where/260630/9 "2008-05-18T15:09:40Z")

</div>

^ My way?

```php
$query = "SELECT * FROM `Table` WHERE ("; 
$conditions = [];

if ($age != '') $conditions[] = "`Age` = '$age'";
if ($ace != '') $conditions[] = "`Race` = '$race'";
if ($gender != '') $conditions[] = "`Gender` = '$gender'";

$query .= implode(' AND ', $conditions).');';

```

---

<div class="post-metadata">

**Author:** ![kdd](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kdd](https://forum.kirupa.com/u/kdd)\
**Post date:** [May 18, 2008, 4:41pm UTC](https://forum.kirupa.com/t/selecting-all-within-where/260630/10 "2008-05-18T16:41:22Z")

</div>

He already got it to work, but using array is a good way also. 🙂

---

<div class="post-metadata">

**Author:** ![Voetsjoeba](https://avatars.discourse-cdn.com/v4/letter/v/ac91a4/32.png) [@Voetsjoeba](https://forum.kirupa.com/u/Voetsjoeba)\
**Post date:** [May 18, 2008, 4:58pm UTC](https://forum.kirupa.com/t/selecting-all-within-where/260630/11 "2008-05-18T16:58:58Z")

</div>

For the love of god, wrap your variables inside mysql\_real\_escape\_string before you stick them in your queries!

---

<div class="post-metadata">

**Author:** ![Charleh](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@Charleh](https://forum.kirupa.com/u/Charleh)\
**Post date:** [May 19, 2008, 9:30am UTC](https://forum.kirupa.com/t/selecting-all-within-where/260630/12 "2008-05-19T09:30:09Z")

</div>

Another easy way is just to use a little case statement trick and just pass through both the variables each time - for instance, assuming that ‘zero’ means the var should be ignored

SELECT \* FROM  
Table  
WHERE  
SomeField = CASE @SomeVar WHEN 0 THEN SomeField ELSE @SomeVar END  
AND  
SomeOtherField = CASE @SomeOtherVar WHEN 0 THEN SomeOtherField ELSE @SomeOtherVar END

This simply checks the value of the variable - when it’s 0 then it compares each data field against itself instead of the variable (effectively ignoring the filter as a field always equals itself)
