简体   繁体   中英

Conditional date in where clause of mysql not filtering

I have written the code above which gets its values form a user. Its working but the date filter isn't working. Instead its displaying all the records in the table even when the dates are provided.

What could be the issue?

$exp_query = "SELECT DISTINCT property_name, property_id, Water_charges, bank_charges, charge_1_description, other_charges_1,
                charge_2_description, other_charges_2, dates ";
$exp_query .="FROM expenses ";
$exp_query .="WHERE
              1=1
              AND (property_name= '".$property."'
                    OR dates BETWEEN '".$datefro."' AND '".$dateto."' )
                ";

There is difference between AND and OR, also how to use OR with AND condition. You have used an OR condition in parentheses, check them.

$exp_query = "SELECT DISTINCT property_name, property_id, Water_charges, bank_charges, charge_1_description, other_charges_1,
                charge_2_description, other_charges_2, dates ";
$exp_query .="FROM expenses ";
$exp_query .="WHERE property_name= '".$property."'
              AND (dates BETWEEN '".$datefro."' AND '".$dateto."' )";

Assuming you are using right condition in your MYSQL query

just use MYSQL DATE() function like::

$exp_query = "SELECT DISTINCT property_name, property_id, Water_charges, bank_charges, charge_1_description, other_charges_1,
                charge_2_description, other_charges_2, dates ";
$exp_query .="FROM expenses ";
$exp_query .="WHERE
              1=1
              AND (property_name= '".$property."'
                    OR DATE( dates ) BETWEEN '".$datefro."' AND '".$dateto."' )
                ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM