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.
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 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;
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
Would be grateful for some help!
Database Tables are set up like this:
id(varchar),
temp(varchar),
humi(varchar),
time(varchar)
Then I thought the user to input the ID, start date and end date.
The problem is how the string in the Time column is formatted, example: 18/03/14: 21:52:36
The user should not have to enter the time, just the date.
I thought it would be possible to do in a similar way:
$result = mysql_query("SELECT * FROM $tbl WHERE id = '$id' AND time BETWEEN '$start%' AND '$stop%'");
But it did not work.
Is it possible to do this with a sql query when the date is stored in such a way?
Regards
. Anders
Edit:
It did not work, probably because I'm doing wrong though = /
If I do this:
$start= "13/02/14 : 12:17:34";
$stop = "13/02/14 : 12:36:18";
$result = mysql_query("SELECT * FROM $tbl WHERE id = '$id' AND tid BETWEEN '$start' AND '$stop'");
..the data will appear as expected
But when I try to to use str_to_date () ,it did not work as I thought, or it did not come out any data at all.
$start= "13/02/14";
$stop = "10/02/14";
$id = "3E000004C6DB8D28";
$result = mysql_query("SELECT * FROM $tbl WHERE id = '$id' AND tid BETWEEN str_to_date('$start%', '%d/%m/%Y') AND str_to_date('$stop%', '%d/%m/%Y')");
edit2:
Do not really know what I was doing weird the first time, but now it works with this code:
$result = mysql_query("SELECT * FROM $tbl WHERE id = '$id' AND tid BETWEEN '$start' AND '$stop'");
You need to use str_to_date():
WHERE id = '$id' AND
tid BETWEEN str_to_date('$start%', '%d/%m/%Y') AND str_to_date('$stop%', '%d/%m/%Y')
Obviously, you can also do this in the application before inserting the values into the query. If so, convert the values to 'YYYY-MM-DD' format.
$query = "SELECT * FROM `tele`.`pedidos`
WHERE `data` LIKE '".date('d/m/Y')."';";
How can I order this by any column? I try:
$query = "SELECT * FROM `tele`.`pedidos`
WHERE `data` LIKE '".date('d/m/Y')."
ORDER BY numero DESC';";
But doesnt work...
Any suggestion?
I bet you are getting syntax error exception right? It's because you lack single quote after the value of date. Try this for clearer view,
$dateHere = date('d/m/Y');
$query = "SELECT * FROM `tele`.`pedidos` WHERE `data` LIKE '$dateHere' ORDER BY numero DESC';";
if you are searching for a specific date, use = not LIKE because it's for pattern matching
$dateHere = date('d/m/Y');
$query = "SELECT * FROM `tele`.`pedidos` WHERE `data` = '$dateHere' ORDER BY numero DESC';";
The single quote in the second query is misplaced. The failure to work could possibly be related to that.
$query = "SELECT * FROM `tele`.`pedidos` WHERE `data` LIKE '".date('d/m/Y')."' ORDER BY numero DESC";
The query syntax is basically right but the single quotes are wrong:
$query = "SELECT * FROM `tele`.`pedidos` WHERE `data` = '".date('d/m/Y')."' ORDER BY numero DESC;";
Actually, this query doesn't make sense. Originally, I read the question that the first query worked but not the second (hence the focus on quotes). Are you trying to say that the date is today? What is data? A string data type or a date data type?
If you wanted to find a match to today and data is a character string, it would be more like:
$query = "SELECT * FROM `tele`.`pedidos`
WHERE date( `data`) = date(now())
ORDER BY numero DESC;";