1

I have been trying to select items from my database that are between two default dates: now and 60 days ago. However all the queries that I try do not work.

Here is what I have tried:

$Now = date("Y-m-d");
$Before = date("Y-m-d", strtotime("-60 days");

// This is try1
$sql = "SELECT * FROM myTable WHERE myTimestamp BETWEEN " . $Before . " AND " . $Now;  

// This is try2
$sql = "SELECT * FROM myTable WHERE myTimestamp >= " . $Before . " AND myTimestamp <= " . $Now; 

I am out of guesses of how to do this. I have looked at the other questions that are the same as this one, but none of the solutions presented work.

Please note: these queries do not give an errors. They just don't retrieve anything. I have also used get_defined_vars() to print the dates onto the page. This is what they show:

[Now] => 2016-01-07
[Before] => 2015-11-08
UndoingTech
  • 709
  • 3
  • 16
  • 1
    *"Please note: these queries do not give an errors."* - Oh? These `" . $Before . " AND " . $Now;` and you're using `2016-01-07` and `2015-11-08` being strings. You're not getting any because you're NOT checking for those syntax errors. Consult http://dev.mysql.com/doc/en/string-literals.html – Funk Forty Niner Jan 07 '16 at 15:04
  • 1
    you're also not querying. – Funk Forty Niner Jan 07 '16 at 15:05
  • 1
    To add to what @Fred-ii- said, and more specifically: you're not executing your queries. There are a lot of unknowns here, such as the API you're using for your database interactions. You also have syntax errors in what you show here (which is why the question was closed) and you may have some other issues. *Right Ralph?* – Jay Blanchard Jan 07 '16 at 15:11
  • 1
    *Right you are Sam* @JayBlanchard – Funk Forty Niner Jan 07 '16 at 15:38

1 Answers1

5

"Please note: these queries do not give an errors." - This " . $Before . " AND " . $Now; and you're using 2016-01-07 and 2015-11-08 being strings and not integers.

Same for " . $Before . " AND myTimestamp <= " . $Now

Plus, those hyphens are interpreted as MINUS, being a mathematical operation.

I.e.: 2016 minus 01 minus 07 etc. resulting in a syntax error.

Therefore, those should read as:

  • WHERE myTimestamp BETWEEN '$Before' AND '$Now' ";

  • WHERE myTimestamp >= '$Before' AND myTimestamp <= '$Now' ";

You're not getting any because you're NOT checking for those syntax errors.

Consult http://dev.mysql.com/doc/en/string-literals.html about string literals.

Also consult When to use single quotes, double quotes, and backticks in MySQL

You're also not querying nor fetching anything and we have no idea which MySQL API you're using to connect with, or whether you did successfully connect to your database.

Consult:

You either use mysqli_fetch_array() or mysqli_fetch_assoc() depending on what you want to do here, and this being a mysqli_ example.

Consult the manuals:

You should take advantage of using MySQL's built-in date/time functions.

and do not mix them together, it doesn't work that way.

Consult: Can I mix MySQL APIs in PHP?

Depending on the MySQL API you are using to connect with, here are a few links for error handling.


Footnotes:

It seems you are new to MySQL, therefore I suggest you have a look at those links I gave you, finding tutorials and Q&A's here on Stack.

You should look into using a prepared statements also:

in order to help against an SQL injection

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    Thank you for your response and all the links provided! They have been very helpful. I did not realize that the dates needed those quotes. I am new to MySQL, so I've been using redbeanphp to do most of the work for me. I'm sorry I did not post the query itself since it caused a lot of confusion. I will provide more details if I ask questions in the future. – UndoingTech Jan 07 '16 at 15:58
  • @UndoingTech You're most welcome and was happy to have been of help, *cheers* – Funk Forty Niner Jan 07 '16 at 15:59
  • 1
    Your answers are always nice to read. On the one hand you explain everything what is going on here in text and give on the other hand very useful links for futher information, so OP and other visitors can learn from it :) – Rizier123 Jan 08 '16 at 00:08
  • 1
    @Rizier123 Thanks *RR*, likewise buddy ;-) – Funk Forty Niner Jan 08 '16 at 00:40