ramiwahdan Posted March 9, 2020 Share Posted March 9, 2020 Hi, I have database stored a field of date. I want to check if the date in the DB field is the same as the current date, how to do it? Code: $oracle = mysqli_real_escape_string($con, $_POST['oracle']); $query = mysqli_query($con, "SELECT indate FROM staff WHERE OracleID ='$oracle'"); $row = mysqli_fetch_array($query); $num_row = mysqli_num_rows($query); if ($num_row > 0) { $curdate = date(); if ($row['indate'] !== $curdate) { $query1=mysqli_query($con, "update staff set ClockedIn = 0, ClockedOut = 0 where OracleID='$session_id'")or die('Error In Session'); header('location:home.php'); } } Quote Link to comment https://forums.phpfreaks.com/topic/310248-check-db-date-with-current-date/ Share on other sites More sharing options...
gw1500se Posted March 9, 2020 Share Posted March 9, 2020 You just have to make sure the date formats match. MySQL only uses yyyy-mm-dd, so: $curdate = date("Y-m-d"); 1 Quote Link to comment https://forums.phpfreaks.com/topic/310248-check-db-date-with-current-date/#findComment-1575291 Share on other sites More sharing options...
Barand Posted March 9, 2020 Share Posted March 9, 2020 Why are you doing a select query then checking the date and updating if not today. All you need is single update similar to this UPDATE staff SET clockedIn = 0, clockedOut = 0 WHERE indate <> CURDATE() Quote Link to comment https://forums.phpfreaks.com/topic/310248-check-db-date-with-current-date/#findComment-1575292 Share on other sites More sharing options...
ginerjm Posted March 9, 2020 Share Posted March 9, 2020 (edited) You should really check the num-rows value before you attempt to do a fetch. Doesn't that make more sense? Perhaps you should also check that the query call itself succeeded before doing that also. Something like: if (query succeeded) if (num_rows test) { (fetch) (process results) } Edited March 9, 2020 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/310248-check-db-date-with-current-date/#findComment-1575293 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.