How can I use a WHERE MySQL clause in a statement which can fetch me all the records? I am settings the WHERE condition conditionally. Like...
if (this) {
$mycondition=1;
} elseif (that {
$mycondition=????
}
'SELECT * FROM table WHERE category='.$mycondition
What should be the value of ???? so that when I pass it to the sql statement it fetches me all the records. I was thinking that WHERE category='*' might work but it does not.
You could just use this
if(this){
$mycondition=" category=1";
}elseif(that{
$mycondition= " 1"; //1 = 'true'. You could also use "1=1
}
'SELECT * FROM table WHERE '.$mycondition
You need to build your WHERE condition piece by piece :
The best answer is not to have a WHERE
clause at all, as suggested as part of peufeu's answer if you don't have a condition to be met:
SELECT * FROM table;
Is perfectly valid.
However, if you are in a situation where you must have a WHERE
clause (or where it's drastically easier to have one), do as Nanne suggests, and put an "always true" condition as your first clause, so something like:
$query = 'SELECT * FROM TABLE WHERE true';
if (this) {
$query = $query . ' AND column=this';
}