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:
- http://php.net/manual/en/mysqlinfo.api.choosing.php
- http://codular.com/php-mysqli (a quick tutorial)
you either use mysqli_fetch_array()
or mysqli_fetch_assoc()
depending on want here, , being mysqli_
example.
consult manuals:
- http://php.net/manual/en/ref.mysql.php
- http://php.net/manual/en/book.mysqli.php
- http://php.net/manual/en/book.pdo.php
- mysql - retrieve timestamps between dates
- https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
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.
- http://php.net/manual/en/function.mysql-error.php (mysql_)
- http://php.net/manual/en/mysqli.error.php (mysqli_)
- http://php.net/manual/en/pdo.error-handling.php (pdo)
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
Post a Comment