MySQL“转换”似乎不起作用

时间:2014-05-07 19:37:44

标签: php mysql

我正在创建一个CSV导出功能的数据库。 我正在使用MySQL转换函数来转换日期,但它似乎没有工作。

我得到的错误是: -

string(140) "SELECT userid,firstname,lastname,email,CONVERT(VARCHAR(10),registrationdate, 101) as registrationdate from users order by userid LIMIT 0,30" 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 'VARCHAR(10),registrationdate, 101) as registrationdate from users order by user' at line 1

代码

<?php
include '../inc/inc.functions.php';
include '../dbconnector.php';
include '../dbpdo.php';
$query = "SELECT userid,firstname,lastname,email,CONVERT(VARCHAR(10),registrationdate, 101) as registrationdate  from users order by userid LIMIT 0,30";
var_dump($query);
$result = mysql_query($query,$db) or die(mysql_error($db));

$array = array();

# Headers
$array[] = array("Serial Number","First Name","Last Name","Email","Registraion Date");

while($row = mysql_fetch_array($result)) {
//    $array[] = $row;
    $array[] = array($row['userid'],$row['firstname'],$row['lastname'],$row['email'],$row['registrationdate']);
}
array_to_csv_download($array,"records.csv",",");

?>

可能是什么问题?任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:0)

MySQL没有CONVERT(..., 101),这是SQL Server格式化日期时间的方式。

在MySQL中,您可以使用DATE_FORMAT('%m/%d/%Y', registrationdate);

SELECT userid,firstname,lastname,email,
       DATE_FORMAT('%m/%d/%Y', registrationdate) as registrationdate 
       from users 
       order by userid 
       LIMIT 0,30