0

I need to select (count) rows with a specific date formatted by YEAR-MONTH-DAY.

i try

 $date = $_GET['date'];

 // requete qui recupere les evenements
$result = "SELECT COUNT(*) FROM evenement WHERE STR_TO_DATE(start, '%Y-%m-%d') = $date";

 // connexion a la base de donnees
 try {
 $bdd = new PDO('mysql:host=;dbname=', '', '');
 } catch(Exception $e) {
 exit('Impossible de se connecter a la base de donnees.');
 }
 $sth = $bdd->query($result);
 echo json_encode($sth->fetchColumn());

where date variable is a date with that format. i want to count rows and return the number of rows with that date.

i also try

$result = "SELECT COUNT(*) FROM evenement WHERE DATE_FORMAT(start, '%Y-%m-%d') = $date";

and

$result = "SELECT COUNT(*) FROM evenement WHERE DATE_FORMAT(DATE(start), '%Y-%m-%d') = $date";

but nothing works

any ideas?

Thanks

9
  • Your code is open to SQL_Injection... Please use prepared statements! Commented Oct 7, 2015 at 14:59
  • the value of $date is? Commented Oct 7, 2015 at 14:59
  • oh thanks for advice @Naruto Commented Oct 7, 2015 at 15:00
  • @Fred-ii- $date get value from an array with dates. wich have that format. Commented Oct 7, 2015 at 15:01
  • 20150907? you didn't specify that. I need specifics. Commented Oct 7, 2015 at 15:02

3 Answers 3

1

You should convert your constant to a date, not the column. So, my first attempt would be:

SELECT COUNT(*)
FROM evenement
WHERE start = DATE('$date');

However, that might not work. You might need to use str_to_date() with the appropriate format.

The reason this method is better is because the optimizer can take advantage of an index on evenement(start).

Sign up to request clarification or add additional context in comments.

6 Comments

Doesn't $date not need be quoted?
@Fred-ii- you are right need quoted. so both answers make my code work. My $date variable need DATE($date). so you suggestion make me think about it and Gordon Linoff 's answer confirmed that.
@NinjTsax Funny you should say that. I had that very same thought earlier: "both answers given will give the OP their solution" - Now, how bizarre is that? ;-)
...and this answer should reflect it before accepting it @NinjTsax I think Gordon should have known this. His experience far exceeds mine ;-)
@Fred-ii- . . . Actually, I was thinking it should be a parameter and not built-into the string. My real point was to do the conversion on the parameter and not on the column.
|
1

The responses and comments all led to this answer

$result = "SELECT COUNT(*) FROM evenement WHERE DATE_FORMAT(DATE(start), '%Y-%m-%d') = DATE('".$date."')"

the problem was that the $date variable need DATE($date) and in SELECT need quotes.

Comments

0

Change like this

$result = "SELECT COUNT(*) FROM evenement WHERE DATE_FORMAT(start, '%Y-%m-%d') = '".$date."'";

You need to enclose $date in '' quotes, else it will perform subtraction like(2014-10-01 = 2003)

2 Comments

and "why" should they "change like this"?
@Fred-ii- in the table im searching for that date. Dates have different format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.