Home > OS >  Can't run Select WPDB to get values between 2 date ranges
Can't run Select WPDB to get values between 2 date ranges

Time:10-04

I am trying to get rows from a custom table I had created in my wp database.

I am trying to get all records that their date_created time value is in a date range. This is part of the function I am building

global $wpdb;
        $start_date = '2021-07-04';
        $end_date = '2021-10-04';
        $table_name = 'statistics_revenues';
        $query = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}$table_name WHERE DATE(date_created) BETWEEN ($start_date AND $end_date)", ARRAY_A);
        $query_results = $wpdb->get_results($query, ARRAY_A);

when I just run a select query with no conditions, everything works, but as soon as I add the part from "WHERE DATE(date_created)...." the query does not get the needed results.

Where did I get it wrong?

CodePudding user response:

Remove parenthesis from between just write BETWEEN $start_date AND $end_date

CodePudding user response:

The main reason for the bug actually was that I didn't understand the wpdb->prepare method.

It basically works as sprintf and I used it in another manner. in any case, Thank you everybody :)

  • Related