2012-02-25 48 views
1

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select1個月

我試圖建立一個MySQL查詢其選擇一個月內的所有活動中選擇所有日期。到目前爲止,我有這樣的:

public static function allMonthlyActivities($db, $startdate, $enddate) { 
     $sql = "SELECT shortdate FROM calendar WHERE date >= $startdate and <= $enddate"; 
     $result = $db->listing($sql); 
     return $result; 
} 

而PHP執行是這樣的:

$list = Calendar::allMonthlyActivities($_DB, '2013-02-01', '2013-02-25'); 

但由於某些原因,我得到這個錯誤:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in... 

這裏是我的數據庫結構:

如何正確地將日期(格式)傳遞給我的SQL語句,以便1個月內的所有日期都返回?

編輯 - 這裏是我的listing功能

public function listing($sql) { 
    $result = mysql_query($sql, $this->_connection); 
    while($row=mysql_fetch_array($result)) { 
     $return[] = $row; 
    } 
    return $return; 
    } 
+0

你可以發佈'上市'功能嗎?或你在哪裏使用'mysql_fetch_array' – Sarfraz 2012-02-25 13:43:03

+0

當然,在這裏你去。 – Michiel 2012-02-25 13:43:50

回答

2

我懷疑你有你的查詢時出現錯誤,更改此:

$result = mysql_query($sql, $this->_connection); 

要:

$result = mysql_query($sql, $this->_connection) or die(mysql_error()); 

shortDate你的數據類型是varchar,嘗試將其更改爲date,看看問題已解決。

另外date是保留關鍵字,使用反碼字符例如`

+0

「shortdate」的內容只是數字,如20,21或22 ... – Michiel 2012-02-25 13:50:54

+0

@Michiel:數據是保留關鍵字,請嘗試將其封裝在反引號字符中'date'''在你的查詢中 – Sarfraz 2012-02-25 13:52:18

+0

但的確,我確實有一個SQL錯誤:'你的SQL語法有錯誤;檢查對應於您的MySQL服務器版本的手冊,以在第一行'<= 2013-02-25'附近使用正確的語法。 – Michiel 2012-02-25 13:53:35

0

你確定你有一個名爲date coloumn因爲MySQL有DATE作爲關鍵字。您的查詢很可能是錯誤的。

+0

我添加了我的表結構的屏幕截圖 – Michiel 2012-02-25 13:46:28

相關問題