【问题标题】:Why is this query ignoring the filter?为什么此查询忽略过滤器?
【发布时间】:2011-12-15 06:53:54
【问题描述】:

如果我删除“and table.field = '$filter' OR table.field='$otherfilter'”,下面的查询可以正常工作。如果我添加这部分,那么它会为我无权查看的人显示结果。我绝对需要添加该部分,以便显示过滤结果。我该如何构造它,以便它使用以下命令向我显示正确的结果:和 table.field = '$filter' OR table.field='$otherfilter'

代码:

  //$filter = "23943409"
    $filter2 = "$username";


  $sql = "SELECT table.field, table2.field, table.field, table.field,
  table.field, table.field, table.field FROM table 
  LEFT JOIN table2 ON table.field = table2.field 
  WHERE table.field='$id' and table.field='$id' 
  and table.field = '$filter' OR table.field='$otherfilter' 
  ORDER BY table.id DESC LIMIT 0, 3";

  $result=mysql_query($sql);

  $query = mysql_query($sql) or die ("Error: ".mysql_error());

  if ($result == "")
  {
  echo "";
  }
  echo "";


  $rows = mysql_num_rows($result);

  if($rows == 0)
  {
  print("");

 }
 elseif($rows > 0)
 {
 while($row = mysql_fetch_array($query))
 {

 $field = $row['field'];

 print("$field");
 }

 }

【问题讨论】:

  • 你注释掉了$filter,所以这个变量是空的。因此,您的查询变为:并且 table.field = ''

标签: php sorting filter


【解决方案1】:

试试AND (table.field = '$filter' OR table.field = '$otherfilter')。注意子句周围的括号。我假设您已经混淆了代码并将实际的字段名称替换为“字段”。

【讨论】:

  • 有趣的是它在其他地方也能正常工作,但不会冒险。将您的建议添加到我使用这个的evertyhwere。
【解决方案2】:

您必须在过滤器周围放置括号:

AND (table.field = '$filter' OR table.field='$otherfilter')

否则你的逻辑是错误的。

【讨论】:

    【解决方案3】:

    您是在用table.field=$id AND table.field = $filter 说“字段是苹果AND 字段是橙色”。希望这只是您对混淆表/字段名的懒惰,但是如果您对这两个值使用相同的 table.field 组合,那么您将排除所有记录,因为单个记录字段中的值不能超过一个一次。

    【讨论】:

      【解决方案4】:

      您可能正在处理andor 之间的优先级问题。试试:

      "SELECT table.field, table2.field, table.field, table.field,
        table.field, table.field, table.field FROM table 
        LEFT JOIN table2 ON table.field = table2.field 
        WHERE table.field='$id' and table.field='$id' 
        and (table.field = '$filter' OR table.field='$otherfilter')
        ORDER BY table.id DESC LIMIT 0, 3
      

      【讨论】:

        猜你喜欢
        • 2021-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-21
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 1970-01-01
        相关资源
        最近更新 更多