0

My long polling isn't working:

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;
    }

}

?>

The problem is giving me a headache: Independent of $timestamp, the result I got from the SELECT is the same, in case I, my last $timestamp = 1439056820 (08/08/2015 15:00:20);

publication_time = timestamp field;
2
  • Have you tried a considerably different timestamp (in the range of months)? and what is the difference in time between themost recent and the oldest publication?
    – Z-WolF
    Aug 20, 2015 at 21:57
  • Tried timestamp of 2018 and the result is the same.
    – Kevin mtk
    Aug 20, 2015 at 23:20

1 Answer 1

0

Your code looks pretty fine to me. have you tried to bind the param as int?

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

Maybe by passing the timestamp as a string you are getting some weird behaviour?

Edit: Since I had some more time, I actually tested the code, and magic:

Warning | 1292 | Incorrect datetime value: '1440162495' for column 'timetest' at row 1

The issue is not with PDO, but the way that you pass data. The immediate solution is to convert your timestamp to a valid date, so something in the lines of:

$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);

Hopefully, this will be enought :)

3
  • 1
    Failed :( , The Select continue showing results with any timestamp; Aug 20, 2015 at 23:17
  • 1
    @MarleneOliveira Updated answer, it should perform as expected
    – Bolovsky
    Aug 21, 2015 at 13:26
  • Ok, i go try this when my house, sorry for bad english
    – Kevin mtk
    Aug 21, 2015 at 15:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.