Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL Select entries with timestamp after X time
    primarykey
    data
    text
    <p>I am attempting to populate a list of records within the last X seconds of the server time.</p> <p>My current query is..</p> <pre><code>mysql_query("SELECT player.name FROM player, spectate WHERE (player.pid = spectate.player) &amp;&amp; spectate.bid=$bid"); </code></pre> <p>This works currently to retrieve all entries. My first thought was to select the time field as well. And then use the mktime() function to find the time difference with the current server time, and only print records with a small difference.</p> <p>But I figured that it would be more effective to include WHERE ($x > $servertime - spectate.time + 1) along with my other statements.</p> <p>It doesn't work as I have it now what am I doing wrong?</p> <p><strong>Function with SELECT</strong></p> <pre><code>function spectateJSON() { $servertime = date("Y-m-d H:i:s"); global $json, $bid; $specResult = mysql_query("SELECT player.name FROM player, spectate WHERE (player.pid = spectate.player) &amp;&amp; spectate.bid=$bid &amp;&amp; (20 &gt; $servertime - spectate.time + 1)"); $specResultCount=mysql_num_rows($specResult); $json.= '"spectate":['; for($i=0; $i&lt;$specResultCount; $i++) { $spectatorName=mysql_result($specResult,$i,"name"); $json.='"'.$spectatorName.'"'; if($i+1 != $specResultCount)$json.=","; } $json.=']'; } </code></pre> <p><strong>function with UPDATE or INSERT</strong></p> <pre><code>if(isset($_SESSION['char'])) { $charId = $_SESSION['char']; $bid = $_GET['bid']; $servertime = date("Y-m-d H:i:s"); $specResult = mysql_query("SELECT player FROM spectate WHERE player='$charId'"); $specResultCount=mysql_num_rows($specResult); if($specResultCount &gt; 0) { $Query=("UPDATE spectate SET bid='$bid' time='$servertime' WHERE player='$charId'"); mysql_query($Query); } else { mysql_query("INSERT INTO spectate (bid, player, time) VALUES ('$bid','$charId','$servertime')"); } } </code></pre> <p>Thanks for your help, any other suggestions / critique is welcome as well =)</p> <p><hr> <strong>Update:</strong> </p> <p>Spectate table entry example.<br> bid: 169<br> player: 1<br> time: 2009-10-20 21:22:54<br></p> <p>Player table entry example:<br> pid: 1<br> uid: 1<br> name: SpikeAi<br> score: 2000<br> wins: 0<br> lose: 0<br> tie: 0</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload