Bad PDO Select语句

时间:2015-08-20 21:37:27

标签: php mysql pdo

我的长期投票不起作用:

server.php

<?php
require 'pdo.php';
set_time_limit(0);
while (true)
{

$requestedTimestamp = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : time();

  clearstatcache();

  $stmt = $pdo->prepare( "SELECT * FROM publication WHERE publication_time > :requestedTimestamp" );
  $stmt->bindParam( ':requestedTimestamp', $requestedTimestamp );
  $stmt->execute();
  $rows = $stmt->fetch(PDO::FETCH_ASSOC);

if (count($rows) > 0) {
$publication = $rows['publication'];
$timestamp = strtotime($rows['publication_time']);
$my = array('publication'=>$publication,'timestamp'=>$timestamp);
$myJSON = json_encode($my);
echo $myJSON;
break;


} else  {
    sleep(2);
    continue;
    }

}

?>

问题让我头疼:独立于$timestamp,我从SELECT得到的结果是相同的,如果是我,我的最后$timestamp = 1439056820 (08/08/2015 15:00:20);

publication_time = timestamp field;

1 个答案:

答案 0 :(得分:0)

你的代码看起来很不错。你试图将param绑定为int吗?

$stmt->bindParam(':requestedTimestamp', $requestedTimestamp, PDO::PARAM_INT);

也许通过将时间戳作为字符串传递,你会得到一些奇怪的行为?

编辑:因为我有更多的时间,我实际上测试了代码和魔术:

  

警告| 1292 |日期时间值不正确:第14行“timetest”列的“1440162495”

问题不在于PDO,而在于您传递数据的方式。直接的解决方案是将您的时间戳转换为有效的日期,所以行中的内容为:

$requestedTimestamp = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : time(); here
//convert unixtime to iso date
$requestedTimestamp = date('c', $requestedTimestamp);
clearstatcache();

$stmt = $pdo->prepare( "SELECT * FROM publication WHERE publication_time > :requestedTimestamp" );
$stmt->bindParam( ':requestedTimestamp', $requestedTimestamp );
$stmt->execute();
$rows = $stmt->fetch(PDO::FETCH_ASSOC);

希望这将是:)