Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6210903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:14:37+00:00 2026-05-24T06:14:37+00:00

Suppose my URL is http://something.com/products.php?brand=samsung&condition=new For the above query I am using isset() and

  • 0

Suppose my URL is http://something.com/products.php?brand=samsung&condition=new

For the above query I am using isset() and $_GET[]) functions along with lots of if-else statements in PHP to generate a sql query for displaying the products which satisfy the search criteria.

For example: if I am dealing with only brand and condition parameters then this is how I will generate the query:

$sql = "select * from products where 1=1 ";
if(isset($_GET['brand']))
{
     if(isset($_GET['condition']))
     {
         $sql = $sql + "and brand=".$_GET['brand']." and condition=".$_GET['condition'];
     }
}
else
{
     if(isset($_GET['condition']))
     {
         $sql = $sql + "and condition=".$_GET['condition'];
     }
     else
     {
         $sql = $sql + ";";
     }
}

Now suppose my URL is having 10 parameters (or more). In this case, using if-else is not at all good. How can I generate the query without using so many if-else statements? Is there any better method/script/library available for doing this thing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:14:37+00:00Added an answer on May 24, 2026 at 6:14 am

    There are a number of ways to do this, but the easiest way would be to loop through the acceptable columns and then append appropriately.

    // I generally use array and implode to do list concatenations. It avoids
    // the need for a test condition and concatenation. It is debatable as to
    // whether this is a faster design, but it is easier and chances are you 
    // won't really need to optimize that much over a database table (a table
    // with over 10 columns generally needs to be re-thought)
    $search = array();
    // you want to white-list here. It is safer and it is more likely to prevent
    // destructive user error.
    $valid  = array( 'condition', 'brand' /* and so on */ );
    
    
    foreach( $valid as $column )
    {
       // does the key exist?
       if( isset( $_GET[ $column ] ) )
       {
          // add it to the search array.
          $search[] = $column . ' = ' . mysql_real_escape_string( $_GET[ $column ] );
       }
    }
    $sql = 'SELECT * FROM TABLE_NAME WHERE ' . implode( ' AND ', $search );
    // run your search.
    

    If you really are trying to get rid of the ‘if’ statements, you could use this:

    $columns = array_intersect( $valid, array_keys( $_GET ) );
    foreach( $columns as $column )
    {
        $search[] = $column . ' = ' . mysql_real_escape_string( $_GET[ $column ] );
    }
    $sql = 'SELECT * FROM TABLE_NAME WHERE ' . implode( ' AND ', $search );
    

    But you may want to run actual benchmarks to determine whether that is a substantially better option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two url for the same file, say http://site.com/fr/page.php and http://site.com/en/page.php .
Suppose I am retrieving a url as follows: string url = http://www.somesite.com/somepage.html HttpWebRequest req
Suppose I was given a URL. It might already have GET parameters (e.g. http://example.com/search?q=question
Suppose I have a url like: http://example.com/get-users which returns a JSON object of all
Suppose I have this script : http://www.example.com/profile.php?uid=12345 And inside this script, there's a send
suppose I have a url like :http://localhost:32432/ When I go to this url I
Let's suppose I have xml like this one: <Server Active=No> <Url>http://some.url</Url> </Server> C# class
I'm trying to implement java like url pattern matching in php. Suppose i have
Say, I have a direct URL to a Twitter post, like $url=http://twitter.com/#!/YourName/status/01234567890123456; or $url=http://twitter.com/YourName/status/01234567890123456;
My problem: I have an iframe from my partner <iframe src=http://foo.com/aff_c?offer_id=28&aff_id=1020&file_id=164&url_id=32></iframe> It´s the formular

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.