2013-03-30 51 views
3

我一直堅持這一段時間,我知道很初學者,但找不到任何類似的問題。SQL命令的類型轉換警告

我想顯示我的最後一個主題的詳細信息,但我收到警告。

*Warning: pg_exec() [<a href='function.pg-exec'>function.pg-exec</a>]: 
Query failed: 
ERROR: operator does not exist: character varying = integer LINE 4: WHERE 
t_cat = 3^
HINT: No operator matches the given name and argument type(s). You 
might need to add explicit type casts.* 

任何幫助表示讚賞

$topicsearh = pg_exec($db, 
    "SELECT t_id, t_subject, t_date, t_cat 
     WHERE t_cat = " . $row['s_id'] . " 
     ORDER BY t_date DESC LIMIT 1" 
);  
if(!$topicsearh){ 
      echo 'Last topic could not be displayed.'; 
} 
else{ 
     while($trow = pg_fetch_assoc($topicsearh)) 
     echo '<a href="topicview.php?id=' . $trow['t_id'] . '">' . $trow['t_subject'] . 
      '</a> at ' . date('d-m-Y', strtotime($trow['t_date'])); 
} 

回答

3

您需要定義FROM表。

SELECT t_id, t_subject, t_date, t_cat FROM TABLE_NAME WHERE... 
          ----------^^^^^^^^^^^^^^^----- 

並且還CON-貓如下。

WHERE t_cat = '". $row['s_id'] ."' 
2

雖然@Dipesh is right about the missing FROM clause,手頭的錯誤指向您的查詢中的另一個問題。 t_cat顯然是character varying類型。因此,您必須將其與匹配的字符串常量進行比較。但是,您正在交付數字常量而沒有單引號。 (或MySQL)傳統上傾向於默默地吞下這樣的錯誤,並做他們認爲「最好」(這不是他們能做的最好的)。 PostgreSQL幸運的不是。它迫使你毫不含糊。

應該是:代替

WHERE t_cat = '" . $row['s_id'] . "' ORDER BY t_date DESC LIMIT 1"

WHERE t_cat = " . $row['s_id'] . " ORDER BY t_date DESC LIMIT 1" 

閱讀手冊中的一章Constants,特別是String ConstantsNumeric Constants