1

I have a query I'm trying to build so that I can filter by car brand. But if a certain session variable exists, it'll ask for an extra part to be added to the query which is basically limiting the results to the logged in branch. Here's the query:

$query_AUCTION = "SELECT * 
          FROM at_auction AS a 
          JOIN at_brands AS b ON a.aCarBrandID = b.bid 
      ORDER BY b.brand $orderx";

... now this works but where can I add a:

"WHERE bid = '{$bid}'"? ...or a... "AND bid = '{$bid}'";

It brings up an error.

2
  • 2
    "brings up an error" - why do you think you should not tell us what that error is?! Commented Jul 11, 2011 at 20:04
  • SORRY it was just a check your syntax error!!! Thanks for answers everyone. Commented Jul 11, 2011 at 22:09

3 Answers 3

2

insert your where clauses BEFORE your order by

$query_AUCTION = "SELECT * FROM at_auction AS a JOIN at_brands AS b ON a.aCarBrandID = b.bid WHERE bid = '{$bid}' ORDER BY b.brand $orderx";
Sign up to request clarification or add additional context in comments.

Comments

1

Use a generic WHERE 1=1 and if you want to insert additional condition:

$query_AUCTION = "SELECT * 
          FROM at_auction AS a 
          JOIN at_brands AS b ON a.aCarBrandID = b.bid
          WHERE 1=1 ";

if ($bid > 0)
   $query_AUCTION .= " AND bid = '{$bid}' ";

$query_AUCTION .= " ORDER BY b.brand $orderx";

1 Comment

using WHERE 1=1 is bad practice. and not needed in this case
1

try this :

$query_AUCTION = "SELECT * 
                  FROM at_auction AS a 
                  JOIN at_brands  AS b ON ( b.bid = a.aCarBrandID ) ";

if( isset( $_SESSION['bid'] ) )
{
    $query_AUCTION.= " WHERE b.bid = '".$_SESSION['bid']."'";
}

$query_AUCTION.= " ORDER BY b.brand $orderx";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.