简体   繁体   English

PHP-将PDO与日期字段一起使用不起作用

[英]PHP - using PDO with a date field not working

so this has been driving me nuts for the last two hours... 所以最近两个小时让我发疯了...

    $sql = 'SELECT * FROM master_rets_table
            WHERE listing_entry_timestamp > "2014-04-01 00:00:00"
            ORDER BY listing_price DESC';
    }
$this->stm = $this->prepare($sql);
$this->stm->execute();

Running this query outside PDO in Navicat works fine ( i get 17 records), but no matter how I try to change this up, it is simply not working in PDO. 在Navicat的PDO外部运行此查询可以很好地工作(我得到17条记录),但是无论我如何尝试更改此查询,它都根本无法在PDO中工作。 I'm using this same code for lots of PDO queries that do not use a date constraint, and they all work wonderfully. 对于许多不使用日期约束的PDO查询,我都使用了相同的代码,而且它们都工作得很好。

fyi, listing_entry_timestamp is a DateTime field fyi,listing_entry_timestamp是一个DateTime字段

EDIT - here is the whole code block 编辑-这是整个代码块

if ($this->city != "" ) {
    $sql = 'SELECT * FROM master_rets_table
            WHERE city = '.$this->city.'
            ORDER BY listing_price DESC';
    }
    else if ($this->subdiv != ""){
    $sql = 'SELECT * FROM master_rets_table
            WHERE subdivision REGEXP '.$this->subdiv.'
            ORDER BY listing_price DESC';
    }
    else if ($this->date_from != "") {
    $sql = "SELECT * FROM master_rets_table WHERE (listing_entry_timestamp > '2014-04-01') ORDER BY listing_price DESC";
    }

$this->stm = $this->prepare($sql);
$this->stm->execute();

$this->rows= $this->stm->fetchAll(PDO::FETCH_ASSOC);
$this->count = $this->stm->rowCount();
$this->rowIdx = 0;

it is working fine for the other two cases...it seems to have something to do with the datetime field or something. 对于其他两种情况,它工作正常……似乎与datetime字段有关。

EDIT [SOLUTION] - Well, it turns out that the live data wasn't being pushed to the development server and i was testing it in Navicat against the live data, so the query looking for yesterday's data couldn't find anything. 编辑[解决方案]-好吧,事实证明没有将实时数据推送到开发服务器,而我正在Navicat中针对实时数据进行测试,因此查找昨天数据的查询找不到任何内容。

PDO has nothing to do with date fields. PDO与日期字段无关。
It works with dates all right. 它可以处理所有日期。

So, look for bugs in your code/data base/environment/whatever. 因此,在代码/数据库/环境/任何地方寻找错误。

My bet for the different databases. 我对不同数据库的赌注。 One you connect with navicat contains 17 records to match and one you connect with PDO contains nones. 与navicat关联的一项包含17条要匹配的记录,而与PDO关联的一项包含无记录。

Second guess is some wrong code in your db class. 第二个猜测是您的db类中有一些错误的代码。

What is driving me nuts is pointless suggestions with wild guesses. 发疯的是毫无根据的猜测。

OK

mysql MySQL的

mysql> create table so_again(d datetime);
Query OK, 0 rows affected (0.23 sec)
mysql> insert into so_again values (now());
Query OK, 1 row affected (0.08 sec)
mysql> select * from so_again where d > '2014-04-01 00:00:00';
+---------------------+
| d                   |
+---------------------+
| 2014-04-02 00:23:16 |
+---------------------+
1 row in set (0.02 sec)

PHP 的PHP

$sql = 'select * from so_again where d > "2014-04-01 00:00:00"';
$stm = $pdo->prepare($sql);
$stm->execute();
$stm = $stm->fetchAll();
var_dump($stm);

ouptut 乌普图

array(1) {
  [0]=>
  array(1) {
    ["d"]=>
    string(19) "2014-04-02 00:23:16"
  }
}

It works. 有用。

at least with raw PDO. 至少使用原始PDO。

  • there is no problem with SQL SQL没有问题
  • there is no problem with PDO PDO没有问题
  • there is no problem with date 日期没有问题
  • there is no problem with quotes (as long as we take 17 returned results for granted. however, in ANSI mode they will cause an error) 引号没有问题(只要我们认为17个返回结果是理所当然的。但是,在ANSI模式下,它们将导致错误)

There can be only problem with your own code/data/input 您自己的代码/数据/输入可能只有问题

Try this: 尝试这个:

$sql = "SELECT * FROM master_rets_table WHERE listing_entry_timestamp > :date ORDER BY listing_price DESC";
$statement = $this->prepare($sql);
$statement->bindParam (":date", "2014-04-01 00:00:00");
$statement->execute();

The only two things that come to my mind are that you might have some kind of casting error in the date parameter, or a logic path error. 我想到的仅有两件事是,date参数中可能存在某种转换错误,或者是逻辑路径错误。 I also noticed that you treat $city as if it was a string, yet you do not quote it. 我还注意到,您将$city当作字符串来对待,但是没有引用它。

Try with: 尝试:

if ($this->city != "" ) {
    $where = 'city = ?';
    $match = $this->city;
} else if ($this->subdiv != "") {
    $where = 'subdivision REGEXP ?';
    $match = $this->subdiv;
} else if ($this->date_from != "") {
    $where = 'listing_entry_timestamp > ?';
    $match = 20140401;
} else {
    trigger_error("No search", E_USER_ERROR);
}

$sql = "SELECT * FROM master_rets_table WHERE {$where} ORDER BY listing_price DESC";
$this->stm = $this->prepare($sql);
$this->stm->execute(array($match));

$this->rows  = $this->stm->fetchAll(PDO::FETCH_ASSOC);
$this->count = $this->stm->rowCount();
$this->rowIdx = 0;

This should both supply a 'recognizable' parameter to PDO and ensure that $sql is indeed populated. 这应该为PDO提供一个“可识别的”参数,并确保确实填充了$sql

Another possibility 另一种可能性

You say, 你说,

$city is preformatted with PDO::quote()...thats an old habit of mine $city已使用PDO :: quote()预先格式化...这是我的一个旧习惯

If it were so, let's suppose that $this->city is initially empty. 如果是这样,让我们​​假设$this->city最初是空的。

Then PDO::quote would make it into an empty quoted string : ie a pair of quote marks with nothing inside. 然后PDO::quote会将其变成一个空的带引号的字符串 :即一对引号,里面没有任何内容。

Then, the comparison $this->city != "" would always return true , because city is not "", but "''". 然后,比较$this->city != ""将始终返回true ,因为city不是“”,而是“”“。

And so the SQL would run a different query from what you'd expect. 因此,SQL将运行与您期望的查询不同的查询。

can you tell me the output when you execute this modified query:

$sql = 'SELECT * FROM master_rets_table
                WHERE (listing_entry_timestamp > :entry_date)
                ORDER BY listing_price DESC';
        }
    $this->stm = $this->prepare($sql);
    $this->stm->execute(array("entry_date"=>"2014-04-01 00:00:00"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM