2014-05-07 48 views
0

我正在創建一個數據庫到CSV導出功能。 我正在使用MySQL轉換函數來轉換日期,但它似乎並沒有工作。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",","); 

?> 

可能是什麼問題?任何的意見都將會有幫助。

+4

爲什麼你不使用DATE_FORMAT? –

回答

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 
+0

@ user3605847嗯,我看不出在你的代碼中的原因。數據庫中的'registrationdate'非空,並且在查詢的'as'部分拼寫正確? –

+0

是的,它被設置爲非空並拼寫正確。 – user3605847

+0

@ user3605847查詢是否在常規SQL提示符下運行會返回正確的結果? –