• Resolved gibcw

    (@gibcw)


    Already using the ability to limit data based on user input following the example:
    SELECT * FROM table WHERE fieldname = '<?php $_POST[formvariable] ?>'

    But how to I add an optional clause based on whether a variable has a value or not.
    For example:

    SELECT * FROM table
    WHERE pu.user_id = <?php $current_user->ID ?>
      AND fieldname = '<?php $_POST[formvariable] ?>'

    If “formvariable” is not set, I would still like to return all the rows from table.
    In other words, how to omit the “and” clause based on the value of “formvariable”?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Eli

    (@scheeeli)

    This would be the simplest way:

    
    AND (fieldname = '<?php $_POST[formvariable] ?>'
     OR '' = '<?php $_POST[formvariable] ?>')
    

    Note the parenthesis, very important ??

    • This reply was modified 8 years ago by Eli.
    • This reply was modified 8 years ago by Eli.
    Thread Starter gibcw

    (@gibcw)

    Thanks… good to learn something new everyday.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Optional where clause based on user input’ is closed to new replies.