简体   繁体   中英

Syntax help! Php and MYSQL

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

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.

Why didn't

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

Work?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM