views:

51

answers:

3

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!

A: 

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.

Jorge Israel Peña
m - Numeric representation of a month, with leading zeros 01 through 12.It's for the function date
Doug
'$m' what is that ?
RageZ
http://php.net/manual/en/function.date.php - And single quotations do not work
Doug
no that's in SQL. i don't think it's a variable. it's something like gmdate('...',$time);'s formatting.
thephpdeveloper
Ah okay, sorry about that.
Jorge Israel Peña
No, you've got the DATE function in a string that you're sending to MySQL -- so it's being executed by the database NOT by PHP.
Eli
A: 

Why didn't

$sql = "SELECT DATE(\"m\", TimeAdded ) AS Date, ColumnName FROM TableName ORDER BY TimeAdded ASC";

Work?

Myles
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 ' TimeAdded ) AS Date, ColumnName FROM TableName ORDER BY TimeAdded ASC' at line 1
Doug
+3  A: 

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%5Fmonth

thephpdeveloper
You know, I thought date() was a php function, I guess it might have just been mysql. THanks!
Doug
no problem! when it's inside the query string, whatever is inside that string is SQL.
thephpdeveloper
date() is a PHP function, but that's not what you wanted. You wanted a MySQL function because everything in $sql is being sent (and exectued) by MySQL.
Eli