...

View Full Version : DATE_FORMAT() with LEFT JOIN Problem



foxfour
03-29-2009, 12:57 AM
I have a server at home I use for testing code and it is running MYSQL version 5.0.27 . When I run the query below it works fine -

$sql = "SELECT voucher AS voucher, DATE_FORMAT(slotDateTime, '%H:%i') AS st, firstName AS fn,lastName AS ln, address1 AS a1, address2 AS a2, address3 AS a3, tel1 AS t1, tel2 AS t2, clientNote AS cn FROM Booking LEFT JOIN Voucher USING (voucher) WHERE DATE_FORMAT(slotDateTime, '%D %M') = '$_POST[seldate]' ORDER BY slotDateTime ASC";

However my website hosting company is running MYSQL version 4.1.22 and that query doesn't work. If I amend it to the one below it works but I only want the time information displayed on the page so the DATE_FORMAT(slotDateTime, '%H:%i') bit is quite important.

$sql = "SELECT slotDateTime AS sdt, voucher AS voucher, firstName AS fn, lastName AS ln, address1 AS a1, address2 AS a2, address3 AS a3, tel1 AS t1, tel2 AS t2, clientNote AS cn FROM Booking LEFT JOIN Voucher USING (voucher) WHERE DATE_FORMAT(slotDateTime, '%D %M') = '$_POST[seldate]' ORDER BY slotDateTime ASC";

The coding is part of an online booking system for a local club and it allows the instructors to look at the client details on booked time slots. I'm pretty sure that if I approach the web hosting company they will not update the MYSQL version they are running or want to charge me an arm and a leg to do so.

Is there another way to write the query that will work in version 4.1.22 ?

Any help appreciated as this is driving me mad.

Old Pedant
03-29-2009, 02:00 AM
I'd fix this in the PHP code.

I don't know PHP, but surely there must be a way in PHP to separate the "D" (day) and "M" (month) parts from the user's input $_POST[seldate], yes?

So then just check for them separately:


... WHERE DAYOFMONTH(slotDateTime) = $seldate_DAY AND MONTH(slotDateTime) = $seldate_MONTH ...

(I made up those variable names--$seldate_DAY, $seldate_MONTH--that I expect you to supply. Note that the should be integers, or at least strings that are integer values.)

The MYSQL functions DAYOFMONTH() and MONTH() date back to at least 3.23.

(And chances are this will be somewhat more efficient than using FORMAT would be, in any case.)

bdl
03-29-2009, 03:54 AM
However my website hosting company is running MYSQL version 4.1.22

Holy crap, really? Can you move? Have you asked if they offer 5.x on an alternate server? Our old web host is using a 'beta' 5.0.x server (even though 5.1 is the current production release) inline with their legacy 4.1 server, and you can use either if you need the updated functions.




SELECT
voucher AS voucher
, DATE_FORMAT(slotDateTime, '%H:%i') AS st
, firstName AS fn
, lastName AS ln
, address1 AS a1
, address2 AS a2
, address3 AS a3
, tel1 AS t1
, tel2 AS t2
, clientNote AS cn
FROM Booking
LEFT JOIN Voucher USING (voucher)
WHERE DATE_FORMAT(slotDateTime, '%D %M') = '$_POST[seldate]'
ORDER BY slotDateTime ASC


This doesn't work in 4.1? You're still using DATE_FORMAT in the WHERE clause in either statement, so I don't get why it doesn't work. Is there an error message returned?




I don't know PHP, but surely there must be a way in PHP to separate the "D" (day) and "M" (month) parts from the user's input $_POST[seldate], yes?
Yes of course you can do this with PHP. I'm more concerned that the OP is actually allowing POST data straight into the SQL statement without validating or escaping it.

Old Pedant
03-29-2009, 09:40 PM
LOL! Darned good point, BDL!

I admit I glossed over that, just thinking he was showing sample code, not the real thing. But I'll bet you are right!

So if he validates the POST data and separates it into INTEGER month and day and uses that as I suggested, he will have accomplished it all: Better performance and no SQL Injection vulnerabilities.

p.s.: Yes, I really knew PHP could do that. I should have said that I just don't happen to know the right functions to call to do so.

foxfour
03-30-2009, 08:43 PM
Thanks for the replies guys. I've had a response form the hosting company and they have offered to move me on to a server running MYSQL version 5.0.67 for a one off fee of 15+VAT ($25). I think I will go with that, I'm not too happy with paying but considering how much of my time I've wasted with this it's probably worth it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum