wpdb->prepare() returns NULL - php

I can't figure out why this is returning "NULL". I've hardcoded the date/time string in to the $scheduledDates variable. In practice that's user input. Everything works fine when I don't prepare the query.
$scheduledDate = "2015-09-01 00:00:00";
$queryString = "SELECT * FROM schedules WHERE event_start > %s AND event_start < %s + INTERVAL 1 DAY";
$scheduled_blocks = $wpdb->get_results( $wpdb->prepare( $queryString, $scheduledDate ) );
The code below works fine, whether I hard code the date/time or not...
$scheduledDate = $_POST['scheduledDate'];
$scheduled_blocks = $wpdb->get_results('SELECT * FROM schedules WHERE event_start > "' . $scheduledDate . '" AND event_start < "' . $scheduledDate . '" + INTERVAL 1 DAY');

Use $wpdb->print_error() to see what errors you get. By the look of your code though, I think the number of placeholders have to be the same as the amount of values you're supplying to the prepare method. Alter your call to this:
$wpdb->prepare($queryString, $scheduledDate, $scheduledDate)

Related

cannot get record which exist between a given time in codeigniter

I want to get records which exists between given start time and end time, start time and end time will be given by user, i'm using the following query for that :
$this->db->where('TIME(start) BETWEEN "' . date('H:i:s', strtotime($start_time)) . '" and "' . date('H:i:s', strtotime($end_time)) . '"');
$this->db->where('TIME(end) BETWEEN "' . date('H:i:s', strtotime($start_time)) . '" and "' . date('H:i:s', strtotime($end_time)) . '"');
$results = $this->db->get('class')->result();
start and end time column is datetime field:
eg. in database
start time : 2020-06-08 06:15:00
end time : 2020-06-08 09:15:00
#user3653474 You can use TIMEDIFF to check if the time provided is between the values of two columns or not. See if the query below does your deed.
$start = date('H:i:s', strtotime($start_time)); // get time from date in desired format
$end = date('H:i:s', strtotime($end_time));
// where $start is between column {start} and {end} AND $end is between column {start} and {end}
$sql = "SELECT * FROM class
WHERE (TIMEDIFF('$start', TIME(start)) >=0 AND TIMEDIFF('$start', TIME(end)) <= 0)
AND (TIMEDIFF('$end', TIME(start)) >=0 AND TIMEDIFF('$end', TIME(end)) <= 0)";
$results = $this->db->query($sql)->result();

PHP query between two dates?

I am trying to set a table up to display out of date tests. So, I have a test that is dated 28/01/2014 and the next test is due on 28/01/2015. As of today's date, this is out of date.
My query looks like the following:
$expiry = date("Y-m-d");
$queryout = "SELECT appliances.*, tests.* FROM (appliances LEFT JOIN tests
ON appliances.ID = tests.Appliance) WHERE Cli_ID = '$useractiveid'
AND `Next Test Due` BETWEEN `Date Tested` AND '" . $expiry . "'";
I want the query to find tests that are out of date where the Next Test Due is out of date.
Here is my answer based on my comments:
$expiry = date("Y-m-d");
$queryout = "SELECT appliances.*, tests.* FROM (appliances LEFT JOIN tests
ON appliances.ID = tests.Appliance) WHERE Cli_ID = '$useractiveid'
AND where Next Test Due < '" . $expiry . "'";

SQL Query using DATE_ADD for time

Hey guys I've tried to figure this out,
I have
$query = "SELECT URL
FROM Matches
WHERE match_date = '" . $currentdate . "'
AND play_datetime < '" . $currenttime . "'
AND DATE_ADD(play_datetime, INTERVAL 3 HOUR) > '" . $currenttime . "'";
I can get the first AND to return, but the second part wont work where i have DATE_ADD, ive tried it by itself to return anything and i cant get it to work!
$current time right now is 06:01:14, its = date(h:i:s);
then in play_datetime it picks up the match_date lists a result, the time in play_datetime is 04:00:00
According to your query :
$currendate have date
and $currenttime have time.
But your column name "play_datetime" suggest that it is having datetime or timestamp .
so if this is right that your query is working but you have to put $currentdatetime instead of $currenttime.
$query = "SELECT URL
FROM Matches
WHERE match_date = '" . $currentdate . "'
AND play_datetime < '" . <timestamp> . "'
AND DATE_ADD(play_datetime, INTERVAL 3 HOUR) > '" . <timstamp> . "'";

Count records from multiple tables in real-time

I want to count a record by current date from different tables and return as one row with different column in the new table. The code will update a record every three hours and insert new record if current date changes. I've current date and time data (2013-05-20 14:12:12) in "created_at" column. Here my current code:
require_once('./db_connect.php');
$dbcon = new db;
//test to see if a specific field value is already in the DB
public function in_table($table,$where) {
$query = 'SELECT * FROM ' . $table . ' WHERE ' . $where;
$result = mysqli_query($this->dbh,$query);
$this->error_test('in_table',$query);
return mysqli_num_rows($result) > 0;
}
//running in background
while (true) {
$select= "SELECT (SELECT CURDATE()) AS time," .
"(SELECT COUNT(tweet_id) FROM tweets WHERE created_at= 'CURDATE() %') AS total_count," .
"(SELECT COUNT(fid) FROM fun WHERE ftime= 'CURDATE() %') AS f_count," .
"(SELECT COUNT(sid) FROM sad WHERE stime= 'CURDATE() %') AS s_count";
$results = mysqli_query( $dbcon, $select );
while($row = mysqli_fetch_assoc($result)) {
$time = $row['time'];
$total = $row['total_count'];
$fcount = $row['f_count'];
$scount = $row['s_count'];
$field_values = 'time = "' . $time . '", ' . 'total_count = ' . $total . ', ' . 'fun_count = ' . $fcount . ', ' . 'sad_count = ' . $scount;
if ($dbcon->in_table('count','time= "' . $time . '"')) {
$update = "UPDATE count SET $field_values WHEN time= '$time'";
mysqli_query( $dbcon, $update );
}
else {
$insert = "INSERT INTO count SET $field_values";
mysqli_query( $dbcon, $insert );
}
}
//update record every 3 hour
sleep(10800);
}
With this code I can't get a count record. The result return | 2013-05-18 | 0 | 0 | 0 |. How can I correct this?
I not familiar with PHP, but you can retrieve the count of all records dated any time today using:
SELECT COUNT(tweet_id)
FROM tweets
WHERE created_at >= curDate()
AND created_at < date_add(curDate(), interval 1 day)
It is equivalent to saying
..
WHERE created_at >= (today at midnight *incusive*)
AND created_at < (tomorrow at midnight *exclusive*)
Update:
The advantage of this method is it is index friendly. While using WHERE DATE(Column) = currDate() works, it can prevent the database from using indexes on that column, making the query slower.
Replace the parts where you have this:
WHERE created_at= 'CURDATE() %'
with this:
WHERE DATE(created_at) = CURDATE()
Your existing WHERE clause is comparing created_at to the string constant CURDATE() %, and they'll never match.
You are comparing against created_at= 'CURDATE() %', which is looking for that exact string, not for the result of a function. If the field created_at is a date, it will never match.
And, you are doing that for all counts.

Select data between two dates?

I'm using a database to store logs, with a column "date" which holds the date it was inserted. The format of the date is "MM/DD/YY". Please can anyone suggest how I would SELECT data in between two certain dates. For example, I tried this:
$from_date = "01/01/12";
$to_date = "02/11/12";
$result = mysql_query("SELECT * FROM logs WHERE date >= " . $from_date . " AND date <= " . $to_date . " ORDER by id DESC");
while($row = mysql_fetch_array($result)) {
// display results here
}
But I guess this doesn't work because the dates aren't numbers. Thanks for the help! :)
Use the BETWEEN keyword:
"SELECT * FROM logs WHERE date BETWEEN '" . $from_date . "' AND '" . $to_date . "'
ORDER by id DESC"
You can cast the fields as dates and then select between from_date and to_date
SELECT * FROM logs WHERE date STR_TO_DATE(date, '%m/%d/%Y') between STR_TO_DATE(from_date, '%m/%d/%Y') and STR_TO_DATE(to_date, '%m/%d/%Y')
The answer to your question depends on the data type that is used to store the date field in the logs table.
SQL (MySQL in your case) is fully capable of comparing dates. Usually, the BETWEEN .. AND .. operator is used but that will not work correctly if the type of date is CHAR (or VARCHAR) - in which case you will need to cast the date field to a DATETIME before comparing.
You need to add single quotes to the date values '01/01/12':
$from_date = "01/01/12";
$to_date = "02/11/12";
$result = mysql_query("SELECT * FROM logs WHERE date >= '" . $from_date . "' AND date <= '" . $to_date . "' ORDER by id DESC");
Change date parameters into Unix timestamps and then compare them. Here is the code:
$from_date = "2019/01/12";
$to_date = "2019/01/15";
$from_date_unix = strtotime($from_date);
$to_date_unix = strtotime($to_date);
$result = mysql_query("SELECT * FROM logs WHERE date >= " . $from_date_unix . " AND date <= " . $to_date_unix . " ORDER by id DESC");
while($row = mysql_fetch_array($result)) {
// display results here
}

Categories