ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
$query = "SELECT DATE_SUB(CURDATE(),INTERVAL 3 DAY)";
$date = MYSQL_QUERY($query) or die("SQL Error Occured : ".mysql_error().':'.$query);
$mysqlquery = "SELECT timesheet.empid, MAX(timesheet.checkin) as 'lastcheckin', employee.lastname, employee.firstname FROM timesheet INNER JOIN employee ON timesheet.empid=employee.empid WHERE 'lastcheckin' < '$date' AND employee.active='y' AND employee.admin='0' GROUP BY empid ORDER BY 'lastcheckin'";
$queryresult = MYSQL_QUERY($mysqlquery) or die("SQL Error Occured : ".mysql_error().':'.$mysqlquery);
What am I doing wrong?
I want it to return timesheet.checkin where it is older than today -3 days.
$query = "SELECT DATE_SUB(CURDATE(),INTERVAL 3 DAY)";
$date = MYSQL_QUERY($query) or die("SQL Error Occured : ".mysql_error().':'.$query);
$mysqlquery = "SELECT timesheet.empid, MAX(timesheet.checkin) as 'lastcheckin', employee.lastname, employee.firstname FROM timesheet INNER JOIN employee ON timesheet.empid=employee.empid WHERE 'lastcheckin' < '$date' AND employee.active='y' AND employee.admin='0' GROUP BY empid ORDER BY 'lastcheckin'";
$queryresult = MYSQL_QUERY($mysqlquery) or die("SQL Error Occured : ".mysql_error().':'.$mysqlquery);
What am I doing wrong?
I want it to return timesheet.checkin where it is older than today -3 days.
TIA
First of all, 'lastcheckin' is a literal string, i.e. data. If you want to quote a column name, you have to use double quotes.
Secondly, you are including $date in the string without quoting it. Depending on the database schema, this may be a bad idea; you should probably use a parameterised query here. It's good practice to always use parameterised queries to include variables in SQL, and if that's not possible for some reason, use DBI::quote to quote the string, rather than just putting quotes around it.
Thirdly, this is only a program fragment. You haven't defined mysql_error()or MYSQL_QUERY() anywhere, so we don't actually know the calling semantics of the routines you're using.
Ok, I double qouted as you said, and I still get the same output. I found a problem in the data types.
SELECT DATE_SUB(CURDATE(),INTERVAL 3 DAY) outputs in the format yyyymmdd
without the DATE_SUB it would be yyyy-mm-dd because DATE_SUB seems to change it to a numeric context.
where 'lastcheckin' is set by a NOW() function so it is in the format yyyy-mm-dd hh:mm:ss
how can I make the two work together? Is there an easy way to modify NOW() and maintain the format for mySQL to compare the dates? I dont really care about the hours for the purpose of the query, it is just looking at absenteeism of more than 2 days +/-.
This is probably fairly easy to do, but I'm relatively new to any type of programming.
This may be something to do with your date display settings?
Quote:
where 'lastcheckin' is set by a NOW() function so it is in the format yyyy-mm-dd hh:mm:ss
how can I make the two work together? Is there an easy way to modify NOW() and maintain the format for mySQL to compare the dates? I dont really care about the hours for the purpose of the query, it is just looking at absenteeism of more than 2 days +/-.
Look at the DATE_FORMAT function on the same link.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.