1

I am running a query on my page and it is returning the wrong results. Here is my code:

$timestamp = time();
$query = "SELECT * FROM videos WHERE expire >= $timestamp AND home = 1 AND active = 1 ORDER BY id DESC LIMIT 1";
$result = mysql_query($query) or die(mysql_error());

if(mysql_num_rows($result) > 0) {
    $row = mysql_fetch_array($result);
    foreach ($row as $key => $value) {
        $$key = $value;
    }
}

The problem is, it is returning the SECOND record, and not the most recent ID. But, strange part is, if I run this in the Query window of MySQL, it returns the correct record.

This is the data on the record it should return: id: 53, videoid: abc123, expire:1335596400, home: 1, active:1

Anyone have any ideas on this?

3
  • Have you tried ordering by the expire field instead? Commented May 9, 2012 at 18:51
  • Maybe it has something to do with your computer's timezone? Commented May 9, 2012 at 18:52
  • It's because you're ordering by DESC Commented May 9, 2012 at 18:52

2 Answers 2

2

1335596400 is 28 april, so cleary is not the result from time(); It seems that you are runnign the query with another timestamp in MySQL (or without timestamp at all)

Sign up to request clarification or add additional context in comments.

1 Comment

Funny how you can look right past something when you've been staring at the monitor for so long... Thanks for helping me see my mistake.
0

use $query = "SELECT * FROM videos WHERE expire >= $timestamp AND home = 1 AND active = 1 ORDER BY id DESC LIMIT 0,1";

instead

$query = "SELECT * FROM videos WHERE expire >= $timestamp AND home = 1 AND active = 1 ORDER BY id DESC LIMIT 1";

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.