How to modify the where clause in this to include other things

How do I modify the WHERE clause in this code:
$expected = array(first_name, last_name, level, language, 'city', 'state', 'zip', );
$query_search = 'SELECT * FROM french;
$where = false;
foreach ($expected as $var) {
if (isset($_GET[$var]) && !empty($_GET[$var])) {
if ($where) {
$query_search .= ' AND ';
} else {
$query_search .= ' WHERE ';
$where = true;
$query_search .= $var . ' = '. GetSQLValueString($_GET[$var], 'text');
to include things like searching multiple columns and IF NULL:
SELECT * from MyTable where (MyCol1 = %s or MyCol1 Is Null) AND (MyCol2 = %s or MyCol2 Is Null)
Where on earth does it go?
Thanks
Brian

My goal is to have a search form and results combination that:
1) allows the user to ignore any of the page’s existing search fields that
they don’t need, (to search with empty form fields);
2) to use whichever search form fields they choose to use even if the
Registrant for whom they are searching did not insert that information when
they registered, (they are filled in the search form but empty in the
database table);
3) and to permit some of the seach fields to search multiple columns, for
example the search form field ‘sonsName’ would look for that information in
three different database table colums, (sonsName1, sonsName2 etc.).
You’ve given me code that permits 1) above to work beautifully. You’ve also
sent along code that should let me accomplish the other two, and though I’ve
been able to recreate below what I want the multiple search column to do, I
can get neither the multiple column nor the IS NULL sections to work in the
code you sent most recently.
How do I take something like this:
$colname_rsSearch = "-1";
if (isset($_POST['first_name_now'])) {
  $colname_rsSearch = $_POST['first_name_now'];
$colname2_rsSearch = "-1";
if (isset($_POST['last_name_now'])) {
  $colname2_rsSearch = $_POST['last_name_now'];
$colname3_rsSearch = "-1";
if (isset($_POST['sonsName'])) {
  $colname3_rsSearch = $_POST['sonsName'];
mysql_select_db($database_connStr_Milo_db, $connStr_Milo_db);
$query_rsSearch = sprintf("SELECT milo.first_name_now, milo.last_name_now,
milo.ws_number, milo.ws_number_3, milo.ws_number_4 FROM milo WHERE
milo.first_name_now = %s AND milo.last_name_now = %s AND (milo.sonsName1 =
%s) ||  (milo.sonsName2 = %s) || (milo.sonsName3 = %s)",
GetSQLValueString($colname_rsSearch,
"text"),GetSQLValueString($colname2_rsSearch,
"text"),GetSQLValueString($colname3_rsSearch,
"text"),GetSQLValueString($colname3_rsSearch,
"text"),GetSQLValueString($colname3_rsSearch, "text"));
$rsSearch = mysql_query($query_rsSearch, $connStr_Milo_db) or
die(mysql_error());
$row_rsSearch = mysql_fetch_assoc($rsSearch);
$totalRows_rsSearch = mysql_num_rows($rsSearch);
?> (I’ve excluded the IS NULL part from my example, but need it in the final
version)
…and make it work using your code below (which permits someone to search
with empty form fields, but does not yet enlighten me on what to group (with
parenthesis) to make the code execute the multiple column search and the IS
NULL parts of the search.
mysql_select_db($database_connStr_Milo_db, $connStr_Milo_db);
$expected = array('first_name_now', 'last_name_now', sonsName'
$query_search = 'SELECT milo.first_name_now, milo.last_name_now,
milo.ws_number, milo.ws_number_3, milo.ws_number_4  FROM milo';
$where = false;
foreach ($expected as $var) {
  if (isset($_POST[$var]) && !empty($_POST[$var])) {
    if ($where) {
      $query_search .= ' AND ';
    } else {
      $query_search .= ' WHERE ';
      $where = true;
  $query_search .= '(' . $var . ' = '. GetSQLValueString($_POST[$var],
'text') .
  ' OR ' . $var . ' IS NULL)';
$search = mysql_query($query_search, $connStr_Milo_db) or
die(mysql_error());
$row_search = mysql_fetch_assoc($search);
$totalRows_search = mysql_num_rows($search);
As always, thank you.

Similar Messages

  • How do I modify the WHERE clause in my SQL query?

    This seems like such a straight-forward part of the report design, but I'm fairly new to Crystal Reports and I only have experience with modifying reports someone else has already written.  In this particular case, I just need to modify the WHERE clause of the SQL query.  I can select Show SQL Query and see what statement is being used to select data, but I can't find where to modify it since it's grayed out.  I see how to change the selection criteria, parameters, grouping, etc...just not the WHERE clause.  The report is linked to a database used for reporting with a table created and populated by a stored procedure.  I don't need to modify the stored procedure because the data I want to filter by is currently in the table--I just don't know how to filter by what I need.  Here's part of the query:
    SELECT "rpt_dist"."startdate", "rpt_dist"."transtype", "rpt_dist"."laborcode", "rpt_dist"."crewid", "rpt_dist"."regularhrs" FROM   "Reporting"."dbo"."rpt_dist" "rpt_dist"
    WHERE  (rpt_dist."transtype" <> 'WORK' AND rpt_dist."transtype" <> 'WMATL') AND rpt_dist."laborcode" LIKE 'S%' AND (rpt_dist."crewid" = 'HOUS' OR rpt_dist."crewid" = 'HOUS2' ...
    I would like to add another crewid to the WHERE clause.  Thanks for any input.

    1.Open the report in the crystal designer
    2.Go to the field explorer(if hidden go to view menu->field explorer)
    3.Rt. click on the database fields->choose database expert
    4.Now you will see 2 columns-Available DataSource  and Selected Tables
    5.Rt. click on the object(ex.command) available in the Selected Tables column->Choose Edit command
    6.A new Modify Command window will appear,here you can edit your SQL Query
    I get to step 4 and I see the two columns including my database and the report table, but there is no command object available.  If I right-click on my table, I can just view the Properties. ??
    As for the other tip to modify the record selection:  I don't see anywhere the other crewid values are set and if I add the one I'm missing, it doesn't modify the existing SQL Query and when I preview the report it throws off the results so that no data displays??
    I'm using Crystal Reports 11.5 if that makes a difference.  Thanks again.

  • How to remove the OR clause in this query?

    Hello Everyone,
    I have this query where i have a lot of joins and in the where clause there is a ISNULL and conditions based on OR clause.
    How to remove the use of the OR clause which may cause performance problems? Can anyone suggest a method to remove the OR clause?
    select RS.PLANNEDSTART,
    OD.ESTSHIPDATE
    FROM TABLE1 RS(NOLOCK)
         INNER JOIN TABLE2 SS(NOLOCK)
         ON RS.ID1             = SS.ID1
         INNER JOIN TABLE3 AM(NOLOCK)
         ON AM.KEYID        = SS.KEYID
         INNER JOIN TABLE4 OD(NOLOCK)
         ON SS.KEYID= OD.KEYID
        INNER JOIN TABLE 8 DR
       ON
    WHERE              (isnull(RS.DATE1,'1970-01-01 00:00:00.000') ='1970-01-01 00:00:00.000'
                     AND OD.DATE1 between @Beginning_Route_Date and @Ending_Route_Date)
                 OR
                 RS.DATE1!='1970-01-01 00:00:00.000'
                   AND RS.DATE1 BETWEEN  @Beginning_Route_Date and @Ending_Route_Date)              
    Regards
    Gautam S
    Gautam S

    this is one way avoiding OR clause
    WHERE CASE WHEN isnull(RS.DATE1,'1970-01-01 00:00:00.000') ='1970-01-01 00:00:00.000'
    THEN OD.DATE1 ELSE RS.DATE1 END between @Beginning_Route_Date and @Ending_Route_Date)
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • How to edit the where clause of View object in search example dynamically

    Hi,
    I am new to OA Framework. Can anybody please tell me how can I assign the where clause dynamically. I want to search only those rows whose creation date is between a 'Date form' and 'Date to' inputs from the search page
    Thanks
    Anupam

    user567342 ,
    I will advice u again pls refer dev guide, there u can find code sample for ur requirement, which can be accomplished using stringbuffer.
    --Mukul                                                                                                                                                                                                                                                                                                                                                                                                           

  • How to retrieve the 'Where Clause' text?

    Hello all,
    I have created a package to retrieve all the DML's executed against the database for the day, for a particular user. DBMS_LOGMNR package was used to retrieve DMLs. But the DMLs retrieved have their 'WHERE' clause text converted to ROWID's. I need to find out the 'WHERE' clause with the parameters.
    I tried retrieving values from the v$sql views, but that information is cryptic and very volatile.
    Is there a way to retrieve the 'Where' clause? If not, is there a way to retrieve the 'WHERE' clause information using a trigger against the corresponding table.
    I am desparately lookin'for a solution. Hope someone out there can help me out.
    Regards,
    Prabu Raghav.

    Hi Andrew,
    Thanks for the suggestion.
    I am using Oracle 8i not 9i.FGA using DBMS_RLS let's me add policies and retrieve all other information about a user except the SQL statement that's executed by the user.
    I want to retrieve the Current sql statement executed against the table by a particular user and record it.
    Any suggestions?
    Regards,
    Prabu Raghav.

  • How to construct the where clause for a nested xml target file

    Post Author: jlpete72
    CA Forum: Data Integration
    I'm having some problems getting the desired results creating a multi-level nested xml file.  Specifically, it does not seem that the where clause in the child schemas respects values from parent schemas.  I'm sure I'm constructing something incorrectly, but am running out of ideas. 
    I am working with the classic company/order/line hierarchy and there are three levels of output schemas in my target xml file, one for company, order header and order line information.
    For testing, I have hardcoded a restriction at the order header line to deal with only one order.  But unless I hardcode values into the where clause at the order line level of the schema, all values are returned for orders belonging to the company defined in the company level.
    I'm trying a where clause at the order line level similar to:
    order_line.customer = order_header.customer and order_line.order_num = order_header.order_num
    If the customer has more than one order in the data file, then all orders for that customer are placed in the detail for the order header.  Only if I hard code the order number in the where clause do I get only the lines for the order specified in the header section.  Not very practical. 
    What am I missing?

    An External Parsed Entity could be used to reference a schema.
    In your DTD:
    <!ENTITY datamodules SYSTEM "file:///c:/Datamodules/Datamodules.xsd">
    Refer the external entity in xml document:
    <datamodules>&datamodules;</datamodules>

  • How to set the where clause of a value set on the basis of a form field

    I am using a DFF(Descriptive FlexField), which needs to display the value of a certain column(say columnA) on the basis of the value of another column(say columnB).
    So i have created a value set which points to the table which has both these columns, and the DFF uses this value set. However, the problem is that I have not put any where clause in the value set, because of which i cannot handle the exact fetch returns more than one rows error.
    The query has to be as follows:
    select ColumnA from tbl where ColumnB = [ a form value ];
    What I want to know is how can i get the value of a certain field of a certain block of the form in the above query.
    Edited by: 981615 on Jan 14, 2013 12:48 AM
    Edited by: 981615 on Jan 14, 2013 12:48 AM

    Just have a look over these two statements if it solves your problem
    one time where clause
    Set_Block_Property('BLOCK_NAME',ONETIME_WHERE,your form item);
    dynamic where clause
    set_block_property('BLOCK_NAME'default_where, your form itme)
    you can where clause at run time from any procedure or some triggers

  • VPD - How to make the where clause a correlated query

    Hi,
    My VPD function has the following where clause:
    where_clause:= ' project_id in (select object_id from pa_project_classes p'||
    ' where p.class_category='||''''||l_cat||''''||
    ' and p.CLASS_CODE='||''''||l_class||''''||
    ' and p.OBJECT_TYPE=''PA_PROJECTS'''||
    The problem is that the subquery is not using the unique key defined on the table pa_project_classes. What I need is something as follows:
    where_clause:= ' project_id in (select object_id from pa_project_classes p'||
    ' where p.class_category='||''''||l_cat||''''||
    ' and p.CLASS_CODE='||''''||l_class||''''||
    ' and p.OBJECT_TYPE=''PA_PROJECTS'''||
    ' and p.object_id=mainquery.project_id ||
    The problem is that since the table pa_project_classes also has a column project_id, I have to prefix the column name by the alias of the main query like 'mainquery.project_id'. However this seems impossible as main query is not unknown .
    I would be grateful if you could suggest any workaround or solution.
    Thanks

    Just have a look over these two statements if it solves your problem
    one time where clause
    Set_Block_Property('BLOCK_NAME',ONETIME_WHERE,your form item);
    dynamic where clause
    set_block_property('BLOCK_NAME'default_where, your form itme)
    you can where clause at run time from any procedure or some triggers

  • How to override the where clause (Bind Variables) in Backing Bean.

    HI
    I am using bind variables in VO and using exeutewithparam to pass the values to bind. Which is working fine.
    But what i want now to override the values pro grammatically from backing_bean of that page.
    How can i pass the values to bind variables from bean.
    M Niaz.

    Hi
    Try this if it works. this is from 10g
    1)create value binding in the backing bean on any action
    FacesContext ctx=FacesContext.getCurrentInstance();
    ValueBinding vb= ctx.getApplication().createValueBinding("#{bindings['account_name1'].inputValue}");
    2) and then set the value for binding
    vb.setValue(ctx,(String)accountInput.getValue());
    Now execute the method with method binding and execute it
    ctx.getApplication().createMethodBinding("#{bindings.ExecuteWithParams.execute}",null).invoke(ctx,null);
    Regards
    Rohit

  • How to write the where clause

    Hi ,
    i needed to write a select query  in which the search criteria is
    a part of a string .
    eg.
    select * from DBTABLE where DESCRIPTION = 'abc'.
    here abc is a part of any string.
    Is the way of writing the select query correct,
    If not what is the correct way?

    >
    Harshit Rungta wrote:
    > Hi ,
    >
    > i needed to write a select query  in which the search criteria is
    > a part of a string .
    >
    > eg.
    > select * from DBTABLE where DESCRIPTION = 'abc'.
    >
    >
    >
    > here abc is a part of any string.
    >
    > Is the way of writing the select query correct,
    > If not what is the correct way?
    Hi,
        select * from dbtable into table i_dbtable where description = 'ABC'.
    note : whenever you are checking a condition the string should be in capital letters within quotes.

  • Values from a Multi-Select in the where clause of a Select statement

    I have a web page that solicits query parameters from the user.
    The selections that the user makes will populate the WHERE clause of a Select statement.
    One of the controls on the page is a multi-select control.
    When this page posts, I would like to execute a Select statement wherein the selected values from this control appear in the .. Column IN ( <list here> ) portion of the WHERE clause.
    This is an extremely common scenario, but I cannot seem to locate a how-to or message thread that addresses this specific case.
    I have an idea that it may involve dynamic SQL or Execute Immediate, but cannot seem to pin down the answer.
    Any help would be greatly appreciated!

    anonymous - As illustrated here: Re: Search on a typed in list of values
    Scott

  • Using if logic in the where clause of a select statement

    I have a select clause. And in the select clause there is a variable all_off_trt that can be 'Y' or 'N'.
    In the where clause I want to make it so that if a form variable is checked and all_off_trt is 'Y' then
    exclude it else if the form variable isn't checked then select it no matter what all_off_trt is.
    Is there any way to include either and if statement or a case statement within the where clause to acheive this? If not is there another way of doing it?
    Basically I am looking for a case statement like this
    case
    when all_off_trt = 'Y' and mail_para.code = 'Y' then false
    else true
    end
    Message was edited by:
    Tugnutt7

    Ok, so that really doesn't solve my problem. I have 3 different fields that I need to do that with. Each combining in a select statement to print an email list, as well as other thing limiting the where clause.
    This is currently what I have, tested and working 100%.
    cursor email_cur is
         select unique p.email,s.all_off_trt,s.all_deceased,s.no_enroll
    from participant p, trialcom t, ethics s
    where p.status='A'
    and p.surname=t.surname
    and p.initials=t.initials
    and s.trial_cd = t.tricom
    and s.centre = t.centre
    and p.email is not null
    and (t.centre in (select code from mail_parameters where user_name=user and mail_para='CENTRE')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='CENTRE'))
    and (t.tricom in (select code from mail_parameters where user_name=user and mail_para='TRIAL')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='TRIAL'))
    and (t.role in (select code from mail_parameters where user_name=user and mail_para='ROLE')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='ROLE'))
    and (p.country in (select code from mail_parameters where user_name=user and mail_para='COUNTRY')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='COUNTRY'))
    and (t.represent in (select code from mail_parameters where user_name=user and mail_para='REPRESENT')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='REPRESENT'));
    This is in a program unit that runs when a button is clicked. At the end of that I need to add on the 3 case statements that help further narrow down the selection of emails to be printed. Then it prints the emails selected from this statement into a file. So it has to be done right in the select statement. The three table variables are the all_off_trt, all_deceased, and no_enroll. The form has 3 checkboxes. One for each, that when checked (giving the variable associated with the checkboxes a value of 'Y') excludes all emails that have a 'Y' in the coresponding table variable.

  • Changing the where clause

    Hi all,
    Can anyone suggest an appropriate answer for this
    I want to change the where condition in Reports (Developer 2000) dynamically (in runtime)
    Thank you

    Hi,
    Try this in you pre-query trigger.
    -- if you want to add to the where clause do this
    -- first store the current where clause
    C_WHERE := get_block_property('your_block', default_where);
    IF <condition> THEN
         c_where := 'desired_condition'||' AND '||C_where;
    -- append your condition to the existing one.
    END IF;
    set_block_property('your_block', default_where,c_where);
    or if you want to totally replace it just use the set_block_property in the trigger.
    Hope this helps.
    Bob

  • Re: [iPlanet-JATO] How to make an OR in the WHERE clause of a SELECT

    Harry,
    If you get a reference to your model, you can set the
    whereClauseOverride with exactly the string you want to use. Keep in
    mind that you are dealing with a the raw SQL where clause, therefore,
    you must use the actual table column names and not the model's logical
    field names as they could be different.
    QueryModelBase queryModel = (QueryModelBase)getModel(PersonModel.class);
    // you must add the "WHERE" as well
    String whereClause = "WHERE firstName LIKE '%max' OR lastName LIKE '%max%'";
    queryModel.setWhereClauseOverride(whereClause);
    Keep in mind that this will override all where criteria: static and
    dynamic where criteria. Static criteria can be declared in your model
    class: setStaticWhereCriteriaString(STATIC_WHERE_CRITERIA).
    You could optionally get the entire SQL statement and replace the
    __WHERE__ token yourself with a string replace technique. In fact, if
    you want to add an ORDER BY, you will need to get the SQL and append to
    the end.
    queryModel.getSelectSQL();
    Hope this helps. Let me know if you have any questions
    craig
    hlamer wrote:
    Hi,
    if I add criterias to my model
    with "SelectQueryModel.addUserWhereCriterion()" they will be concated
    with AND.
    How can I add a criteria with OR?
    I want something like:
    SELECT personId, firstName, lastName
    FROM Person
    WHERE firstName LIKE "%max"
    OR lastName LIKE "%max%"
    Thanks.
    Harry
    For more information about JATO, including download information, please visit:
    http://developer.iplanet.com/tech/appserver/framework/index.jsp

    Rewritten your query wuth tags as well
    &#123;code&#125;select  "P_CIRCUITS_FIBER"."PORTS_1",
            "P_CIRCUITS_FIBER"."PORTS_2",
            CASE WHEN CPORT.ID = (select REGEXP_SUBSTR(ports_1,'[^,]+',1,1) from p_circuits_fiber)
                 THEN "CPORT"."PORT_NO" END PORT_NO_1,
            CASE WHEN CPORT.ID = (select REGEXP_SUBSTR(ports_2,'[^,]+',1,1) from p_circuits_fiber)
                 THEN "CPORT"."PORT_NO" END PORT_NO_2
    from    "CPORT",
            "P_CIRCUITS_FIBER"
    where   p_circuits_fiber.fiber_id = :P4000_CIRCUIT_NO
    and     (select REGEXP_SUBSTR(ports_1,'[^,]+',1,1) from p_circuits_fiber) = "CPORT"."PORT_NO"  "PORT_NO_1"
    and     (select REGEXP_SUBSTR(ports_2,'[^,]+',1,1) from p_circuits_fiber) = "CPORT"."PORT_NO"  "PORT_NO_2"&#123;code&#125;
    Now we can see two columns references which should not be there.
    I even not sure to understand why those two conditions are in the WHERE clause. But for sure, CPORT.ID cannot be equal to two values unless they are same.
    Is there only one row in p_circuits_fiber ?
    Nicolas.

  • How to change recordset bahaviour to accept dynamic column names in the where clause

    Hi
    im using php-mysql and i make a recordset and i want to make the column names in the where clause to be dynamic like
    "select id,name from mytable where $tablename-$myvar";
    but when i do this my i break the recordset and it disappear
    and when i use variables from advanced recordset it only dynamic for the value of the column not for the column name
    and when i write the column name to dynamic as above by hand it truns a red exclamation mark on the server behaviour panel
    so i think the only way is to change the recordset behaviour is it? if so How to make it accept dynamic column names?
    thanks in advance.

    As bregent has already explained to you, customizing the recordset code will result in Dreamweaver no longer recognizing the server behavior. This isn't a problem, but it does mean that you need to lay out your dynamic text with the Bindings panel before making any changes. Once you have changed the recordset code, the Bindings panel will no longer recognize the recordset fields.
    Using a variable to choose a column name is quite simple, but you need to take some security measures to ensure that the value passed through the query string isn't attempting SQL injection. An effective way of doing this is to create an array of acceptable column names, and check that the value matches.
    // create array of acceptable values
    $valid = array('column_name1', 'column_name2', 'column_name3');
    // if the query string contains an acceptable column name, use it
    if (isset($_GET['colname']) && in_array($_GET['colname'], $valid)) {
      $col = $GET['colname'];
    } else {
      // set a default value if the submitted one was invalid
      $col = 'column_name1'
    You can then use $col directly in the SQL query.

Maybe you are looking for