【问题标题】:Type cast warning for SQL commandSQL 命令的类型转换警告
【发布时间】:2013-03-20 20:22:44
【问题描述】:

我已经在这个问题上停留了一段时间,我知道是初学者,但找不到任何类似的问题。

我正在尝试显示我上一个主题的详细信息,但我收到了警告。

*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']));
}

【问题讨论】:

    标签: php html sql postgresql types


    【解决方案1】:

    虽然@Dipesh is right about the missing FROM clause,手头的错误指向您查询中的另一个问题。 t_cat 显然是 character varying 类型。因此,您必须将其与匹配的 字符串常量 进行比较。但是您正在提交一个没有单引号的数字常量

    PHP(或 MySQL)传统上倾向于默默地吞下这些错误并做他们认为“最好的”(这不是他们能做的最好的)。幸运的是,PostgreSQL 没有。它迫使你明确。

    应该是:

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

    代替:

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

    阅读手册中的Constants章节,特别是String ConstantsNumeric Constants

    【讨论】:

      【解决方案2】:

      您需要定义FROM 表。

      示例

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

      还有 con-cat 如下。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-04
        • 2021-07-19
        • 1970-01-01
        • 2021-11-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多