2013-03-30 49 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