I am trying to count logins by date range by counting how many times the auto integer (id) appears between a start and end date.
I get the start date and end date from a form in a previous page (y-m-d).
$start_date=$_POST['start_date']; /*in this case its "2014-10-10"*/
$end_date=$_POST['end_date']; /*in this case its "2014-10-20"*/
$sql = <<<SQL
SELECT id, COUNT(*) as login_count FROM `usage`
GROUP BY id
WHERE date
BETWEEN $start_date AND $end_date
SQL;
However I keep getting the following syntax error
"There was an error running the query [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE date BETWEEN 2014-10-10 AND 2014-10-20' at line 3]"
What am I doing wrong?
try it
$sql = <<<SQL
SELECT id, COUNT(*) as login_count FROM `usage`
WHERE date
BETWEEN '$start_date' AND '$end_date'
GROUP BY id
SQL;
your $sql should be :
$sql = <<<SQL
SELECT id, COUNT(*) as login_count FROM `usage`
WHERE date
BETWEEN $start_date AND $end_date
GROUP BY id
SQL;
as GROUP BY CLAUSE is AFTER WHERE syntactically
You need to put ''s around date literals in sql like so:
$response = mysql_query("SELECT * FROM `db`.`$sql_table` WHERE (date BETWEEN '$end_date' AND '$start_date') ORDER by id ASC ")or die(mysql_error());
Firstly you should quote your dates '$start_date' AND '$end_date' and you should definitely make sure those values are escaped to avoid SQL injection.
You have mistakes in your SQL. Try this:
SELECT id, COUNT(*) as login_count FROM `usage`
WHERE (date BETWEEN $start_date AND $end_date)
GROUP BY id;
Related
I have the following date field, I need to sort by newest date.
Please help me to solve this.
tried the following query but it's not getting the correct output.
17/12/2014
26/01/2016
19/11/2014
30/06/2014
I need to sort in the following format :
26/01/2016
17/12/2014
19/11/2014
30/06/2014
Here is my code.
$queryold="SELECT * FROM tablename order by STR_TO_DATE(column name,'%m/%d/%Y')";
your code is not working because you have dd/mm/yyyy format. so you need first date then month in conversation
$queryold="SELECT * FROM tablename order by STR_TO_DATE(column_name,'%d/%m/%Y')";
If your column's type is 'datetime' you just have to run this query:
$query = "SELECT * FROM tablename ORDER BY datecolumn DESC";
If it's a varchar the good query is:
$query = "SELECT * FROM tablename ORDER BY CONVERT(datetime, datecolumn) DESC";
I am trying to select an unixtimestamp column from my database, as readable date time:
SELECT count(*) FROM users WHERE user_by="Admin" AND expire(FROM_UNIXTIME(unixtime),'%Y-%m-%d')='2015-10-02'
Above gives me this error:
#1305 - FUNCTION database_maindb.expire does not exist
How can I select the unixtimestamp from column expire as datetime, in the format: year-month-date?
assuming your database field is called "expire":
SELECT count(*) FROM users WHERE user_by="Admin" AND FROM_UNIXTIME(expire,'%Y-%m-%d')='2015-10-02'
see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime for details
You don't need to select is as the YMD date stamp, you could do something such as (using your tags)
$query = "SELECT * FROM `table`;";
$result = pdoObject->queryMethod($query);
$result= pdoObject->fetchAssocMethod($result);
$date = date("Y-m-d", $result['epochCol']);
I hope this helps.
I have 5 records in mysql database and these records have recorded date within this date interval.
$year=2015;
$month=8;
$datefrom=1;
$dateto=31;
$startdate='$year-$month-$datefrom 00:00:00';
$enddate='$year-$month-$dateto 23:59:59';
So I write a query to get these records out like this:
$sql = "SELECT id FROM newpost WHERE email=:email AND :startdate <= poststart <= :enddate AND postapproved=1";
Given that poststart column in table newpost has SQL Datetime format like this: "Y-m-d H:i:s".
But when I changed variable $year = 2016, I still got 5 results? It should return no record. Because those 5 records are recorded between 1 - 31 August 2015.
So I thought I did something wrong in the sql query especially the comparing date part but I could not configure out how to fix it?
Please help!
You can use BETWEEN in your query
$sql = "SELECT id FROM newpost WHERE email=:email AND (poststart BETWEEN :startmonth AND :endmonth) postapproved=1"
Use single quotes to wrap your date values
$sql = "SELECT id FROM newpost WHERE email=:email AND poststart BETWEEN ':startdate' AND ':enddate' AND postapproved=1";
A couple quick things to check to make sure it's not a syntactical error:
Your variable names don't match up. You defined startdate and enddate, but then in the query you used startmonth and endmonth.
You should probably also use leading zeros in your month and day, i.e.:
$month='08';
$datefrom='01';
I have a mysql datetime field that stores dates in the form '2013-12-25 00:00:00'
I need to select all records for any month in the table with a query like:
$sql = "SELECT *
FROM `images`
WHERE (photodate BETWEEN '2003-11-01 00:00:00' AND '2003-12-03 00:00:00')
ORDER BY photodate DESC
LIMIT 30";
The above select query does the job fine.
In order to change the dates, I need to replace the '2003-11-01 00:00:00'AND'2003-12-03 00:00:00' with variables, so I set a variable with input data from two drop down lists for $startyear and $startmonth and convert it to what I think is the correct form using:
$startdate = $startyear."-".$startmonth."-01 00:00:00";
I do the same to the $enddate by adding 1 to the $startmonth.
My code then becomes:
$sql = "SELECT *
FROM `images`
WHERE (photodate BETWEEN $startdate AND $enddate)
ORDER BY photodate DESC
LIMIT 30";
This does not work at all and gives a MySQL error. Having struggled with it for a month and finding nothing on any forum that uses variables instead of text, I am totally at a loss as to how it could be done. All help appreciated.
You are vulnerable to SQL injection attacks, which is why it's not working. You're producing the literal query
... WHERE (photodate BETWEEN 2003-11-01 00:00:00 AND 2013-12-03 00:00:00)
The 2003-11-01 and 2013-12-03 will be interpreted as a series of mathematical subtractions, and the 00:00:00 will be a simple flat-out syntax error. You need to, at bare minimum, quote those values:
... WHERE (photodate BETWEEN '$startdate' AND '$enddate')
^----------^-----^--------^--- note the quotes
so that mysql can see the WHOLE date as a date value, and not some arbitrary broken strings.
I guess you're missing some apostrophes... try this:
$sql = "SELECT * FROM images WHERE (photodate BETWEEN '$startdate' AND '$enddate') ORDER BY photodate DESC LIMIT 30";
You could have problems with the logic. In $enddate doesn't adding 1 to the start month give you 13?
Try printing out the contents $sql when the variables are in and see how it compares to the working $sql.
Please add apostrophes your query (and sanitize your variables using mysql_real_escape_string, PDO bind values, mysqli_real_escape_string) :
$sql = 'SELECT * FROM 'images' WHERE (photodate BETWEEN '.$startdate.' AND '.$enddate.') ORDER BY photodate DESC LIMIT 30';
A little reminder, you shall NOT use MySQL (deprecated, old.. and not that fast), if you're using MySQLi or going to use it, please sanitize your variables like this, as Marc B said it could break your script and your app security :
<?php
// Starting MySQLi Connection
$db = mysqli_connect("host", "user", "password", "dbname");
// Sanitizing your variables
$startdate = mysqli_real_escape_string($db, $startdate);
$enddate = mysqli_real_escape_string($db, $enddate);
// Query
$sql = "SELECT * FROM 'images' WHERE (photodate BETWEEN ".$startdate." AND ".$enddate.") ORDER BY photodate DESC LIMIT 30";
// Doing the query and print the result array
$var = mysqli_query($db, $sql);
print_r($var);
// Closing connection
mysqli_close($db);
?>
Please refer to to this for PDO way or to this for MySQLi way, you can also check the MySQL_real_escape_string into PHP doc but MySQL functions are deprecated since PHP 5.5
Original:
$sql = "SELECT DATE(TimeAdded) AS Date, $column_name FROM Codes ORDER BY TimeAdded ASC";
Altered:
$sql = "SELECT DATE("m", TimeAdded ) AS Date, ColumnName FROM TableName ORDER BY TimeAdded ASC";
TimeAdded was added using NOW() and basically, I am trying to make it months. The problem I have here is the quotations is messing it up. I tried using \ to get rid it but no good. Also tried this:
mysql_real_escape_string( DATE("m", TimeAdded ) )
More info: http://php.net/manual/en/function.date.php The date function uses m to format in months. It's a function from PHP, I think.
Update: I mixed the data() for php with mysql, no wonder!
How about using the MONTH function in MySQL?
$sql = "SELECT MONTH(TimeAdded) AS `Date`, `ColumnName` FROM `TableName` ORDER BY `TimeAdded` ASC";
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_month
Why didn't
$sql = "SELECT DATE(\"m\", TimeAdded ) AS Date, ColumnName FROM TableName ORDER BY TimeAdded ASC";
Work?
If it's a PHP function, then why not set that to a PHP variable before your query and then inject it into the query as you did the other variables? Just a thought.