MySQL Forums
Forum List  »  Newbie

Re: Query table and retriving results
Posted by: Phillip Ward
Date: May 27, 2016 05:08AM

Quote

$query = "select 'Voting_Start_Date','Voting_End_Date' from 'j642_dcc_treasurer' where 'Entry_Date' like '".date(Y)."%'";
If Entry_Date is a Date data type, why are you [implicitly] converting it into a String (of unknown format) and then doing a wild-card comparison on that String value, based on the user-entered "date" value, also in unknown format?

Rule #1 of Date fields.
Date fields have no format.

Only that which MySQL (and, possibly, the I.E.E.E.) keeps to itself and certainly nothing like anything that you or I would call a "date".

... 
"where Entry_Date = '" . formatTheDateSoThatMySqlCanMakeSenseOfIt( enteredDate ) . "' " 
...

Why wouldn't MySQL be able to make sense of the entered "date"?
Because it has to interpret the "character representation of a date" entered by the User into what it considers to be a Date value.

What date does "01-02-03" represent?

Remember; when you're building an SQL statement, you're constructing a String that just happens to be meaningful to your DBMS.
Always be explicit with date formatting, so that there's zero risk of ambiguity.
Or use Parameterised Queries.

Regards, Phill W.

Options: ReplyQuote


Subject
Written By
Posted
Re: Query table and retriving results
May 27, 2016 05:08AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.