php - MySQL query between two dates as timestamps -


i have been trying select items database between 2 default dates: , 60 days ago. queries try not work.

here have tried:

$now = date("y-m-d"); $before = date("y-m-d", strtotime("-60 days");  // try1 $sql = "select * mytable mytimestamp between " . $before . " , " . $now;    // try2 $sql = "select * mytable mytimestamp >= " . $before . " , mytimestamp <= " . $now;  

i out of guesses of how this. have looked @ other questions same one, none of solutions presented work.

please note: these queries not give errors. don't retrieve anything. have used get_defined_vars() print dates onto page. show:

[now] => 2016-01-07 [before] => 2015-11-08 

"please note: these queries not give errors." - " . $before . " , " . $now; , you're using 2016-01-07 , 2015-11-08 being strings , not integers.

same " . $before . " , mytimestamp <= " . $now

plus, hyphens interpreted minus, being mathematical operation.

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

therefore, should read as:

  • where mytimestamp between '$before' , '$now' ";

  • where mytimestamp >= '$before' , mytimestamp <= '$now' ";

you're not getting because you're not checking syntax errors.

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

also consult when use single quotes, double quotes, , backticks in mysql

you're not querying nor fetching , have no idea mysql api you're using connect with, or whether did connect database.

consult:

you either use mysqli_fetch_array() or mysqli_fetch_assoc() depending on want here, , being mysqli_ example.

consult manuals:

you should take advantage of using mysql's built-in date/time functions.

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

consult: can mix mysql apis in php?

depending on mysql api using connect with, here few links error handling.


footnotes:

it seems new mysql, therefore suggest have @ links gave you, finding tutorials , q&a's here on stack.

you should using prepared statements also:

in order against sql injection


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -