SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    May 2002
    Location
    Brandon, MS
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ignore value in mysql query

    Is there any value/variable that can be placed in a mysql query that will simply be ignored? Im using php to send values to a mysql query. The values are submitted from an html form. If value is left empty on the form I still want to be able to do a simple mysql query like;

    $result = mysql_query("SELECT * FROM table WHERE ((option1 LIKE "$option1%")
    AND (option2 = $var2) AND (option3=)");

    If option3 is empty I want it to be ignored rather than search for results where option3 is
    empty or null. I hope that makes sense.

  2. #2
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would simply use something like the following:
    PHP Code:
    <?php
    // Check to make sure at least one of
    // the variables contains a value:
    if((empty($var1))&&(empty($var2))&&(empty($var3))){
         exit(
    '<p>Sorry, no search parameters defined.  Please try again.</p>');
    }
    // Check if variable1 is set:
    if(isset($var1)){
         
    $option1="(option1 LIKE '%{$var1}%')";
    }
    else{
         
    $option1='';
    }
    // Check if variable2 is set:
    if(isset($var2)){
         
    $option2="AND (option2='{$var2}')";
    }
    else{
         
    $option2='';
    }
    // Check if variable3 is set:
    if(isset($var3)){
         
    $option3="AND (option3='{$var3}')";
    }
    else{
         
    $option3='';
    }
    // Combine the results into a query:
    $result=mysql_query("SELECT * FROM table WHERE {$option1} {$option2} {$option3}");
    }
    ?>


    -Colin
    Last edited by Aes; May 30, 2002 at 22:23.
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •