Page 1 of 1

Negating a where clause

Posted: Thu Jan 26, 2012 10:40 am
by markyboy17
OK i have a page which posts to three pages within it like this
http://localhost/EDI_Project/DisplayRes ... orders=yes
http://localhost/EDI_Project/DisplayRes ... nvoice=yes
http://localhost/EDI_Project/DisplayRes ... credit=yes

each one of these links has its own button and code

at the moment i have a basic select stored proc in SQL and it prints a table depending on what one of the above URL's is loaded.
i am now creating a filter for each on of these pages using a drop down
my question is would it be possible to add a where clause to the stored procedure but then if nothing is entered as a parameters it would just display all the records as before. this would really shorten my code if i can just have a where clause that when nothings entered it takes all rows.
thanks in advance

Re: Negating a where clause

Posted: Thu Jan 26, 2012 10:44 am
by Celauran
You could try something like this:

Code: Select all

$where = 1;
if (isset($_GET['where']))
{
    $where = $_GET['where'];
}

$sql = "SELECT foo, bar FROM tablename WHERE {$where}";

Re: Negating a where clause

Posted: Fri Jan 27, 2012 4:02 am
by markyboy17
Thanks but that didn't work
i have solved the problem so if anyone is wondering you have to change the query in the stored proc to this

create proc SelectPracticeIf (@name char(50))
as
if (@name = '')
begin
select customer_id,name,sales_rep,credit_limit from customer
end
else
begin
select customer_id,name,sales_rep,credit_limit from customer WHERE name=@name
end

and then in the php i have this

Code: Select all

$dropdown = $_POST['DropDownList'];
                if(!(isset ($dropdown)))
                {
                    $dropdown = '';
                }
$sql = "SelectPracticeIf ?";
                    $params = array(&$dropdown);
                    $results = sqlsrv_query($conn,$sql,$params);
i appreciate your help thought i would post this if anyone else was wondering