-1

我正在努力找出为什么我的查询返回零行。我已经广泛尝试在这个问题上解决这个问题,但没有得到体面的结果。

它的要点是:

    $mydate=date("Y-m-d",strtotime("-3 months"));

    $foo_query=$DBH->prepare("SELECT * FROM BarTable WHERE postdate = :postdate AND postdate > '$mydate' ORDER BY postdate DESC");
    $foo_query->execute( array('postdate' => $_REQUEST['postdate']) );

编辑:这个查询应该说取日期,将其设置为过去三个月,并将其命名为 $mydate。然后从 BarTable 中取出 postdate 大于 $mydate 的所有字段,然后执行查询。

刚刚有人向我指出,我所说的是我正在选择 postdate 等于 3 个月前并且大于$mydate.

我不明白我怎么这么说。:postdate不等于 3 个月前,因此postdate = :postdate不能选择 postdate 等于 3 个月前的行。

为了正确显示我的行,我之前输入了WHERE postdate > '$mydate'.

如何键入postdate = :postdate AND postdate > '$mydate'以便我使用参数并确保根据大于选择数据$mydate

4

4 回答 4

1

如果我理解正确(我完全不确定),你想要OR而不是AND

$foo_query=$DBH->prepare("SELECT * FROM BarTable WHERE postdate = :postdate OR postdate > '$mydate' ORDER BY postdate DESC");

这样,您将同时获得 3 个月前的帖子和发布在:postdate.

于 2012-10-03T19:51:01.307 回答
1

您需要为postdate. 此外,要满足两个条件postdate = :postdate and postdate > $mydate,您需要OR在查询中使用。

$mydate=date("Y-m-d",strtotime("-3 months"));
$foo_query=$DBH->prepare("SELECT * FROM BarTable WHERE postdate = :postdate OR postdate > '$mydate' ORDER BY postdate DESC");
$foo->bindParam(':postdate', $_REQUEST['postdate'], PDO::PARAM_STR);
$foo_query->execute();

或与您在问题中提到的类似,您:在分配时错过了:postdate,

 $mydate=date("Y-m-d",strtotime("-3 months"));
 $foo_query=$DBH->prepare("SELECT * FROM BarTable WHERE postdate = :postdate OR postdate   '$mydate' ORDER BY postdate DESC");
 $foo_query->execute( array(':postdate' => $_REQUEST['postdate']) );
于 2012-10-03T19:51:19.667 回答
1

如果你想做参数绑定,你可以试试这个,假设你的 postdate 是一个日期字段

$foo_query=$DBH->prepare("
   SELECT * FROM BarTable 
   WHERE postdate = :postdate AND postdate > '$mydate' ORDER BY postdate DESC");
$foo_query->bindParam(':postdate', $_REQUEST['postdate'], PDO::PARAM_STR);
$foo_query->execute(); 

但是,如果您的 postdate 是时间戳,它应该如下所示:

$foo_query=$DBH->prepare("
   SELECT * FROM BarTable 
   WHERE postdate = :postdate AND postdate > '$mydate' ORDER BY postdate DESC");
$foo_query->bindParam(':postdate', $_REQUEST['postdate'], PDO::PARAM_INT);
$foo_query->execute(); 
于 2012-10-03T20:16:36.503 回答
0

我推荐“之间”:

$foo_query=$DBH->prepare(
  "SELECT * FROM BarTable 
  WHERE postdate between ':somedate' AND '$mydate' 
  ORDER BY postdate DESC");
于 2012-10-03T19:54:16.283 回答