0

I am trying to see if a field (end_date) in a MySQL database in the form of 2015-6-11 21:28:02 is greater than the current time. Essentially, is that field in the future or not.

Here is my PHP Code:

$current_time = time();
$sql = "SELECT member_id, course_id, FROM ".TABLE_PREFIX."vbc_status WHERE end_date < NOW()";
$result = mysql_query($sql, $db);
while ($row = mysql_fetch_assoc($result)) {
    $echo $row['member_id'];
}

I keep getting an error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /path/to/script.php on line 373.

Line 373 is the while ($row = mysql_fetch_assoc($result)) { part.

Any ideas of how this should be fixed?

6
  • 2
    What exactly is the error message you get? Commented Jun 4, 2015 at 18:33
  • Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /path/to/my/script.php on line 373 Commented Jun 4, 2015 at 18:35
  • 1
    Ugh. You're using mysql_query, aren't you? You're calling that function incorrectly, wrong arguments, the query string itself is probably fine. Keep in mind a modern database driver like PDO is not hard to learn. A guide like PHP The Right Way can help explain best practices. Commented Jun 4, 2015 at 18:35
  • if you have a moment please read My Post about how to ask sql questions... it has a lot of good tips and tricks to help you ask a question. Commented Jun 4, 2015 at 18:37
  • 1
    Since your code is busted and not working, might as well do it correctly in PDO. It takes literally two lines to covert code like this. Commented Jun 4, 2015 at 18:39

3 Answers 3

3

Why not skip all that and just do this:

WHERE end_date < NOW()

Converting with UNIXTIME is extremely abusive on your database, it needs to evaluate that for every row in the table and cannot use indexes. The date field itself will evaluate very quickly if indexed.

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

8 Comments

Changed it to $sql = "SELECT member_id, course_id, FROM ".TABLE_PREFIX."vbc_status WHERE end_date < NOW()"; and I'm still getting the error.
Yes, because you're not calling the query function correctly. Read the mysql_query documentation to see how you're supposed to call it, as it requires a valid connection handle unless one has been established previously, and note the big red box.
looks like you have an issue with how you are executing your query.. also echo $sql after you create it
So, SELECT member_id, course_id FROM AT_vbc_status` WHERE end_date > NOW()` works fine when I use this directly in PHP MyAdmin. However, this same logic isn't working when I put it in PHP. I'm also using similar logic later on in the code and it works fine. I'm going to update my original post with more code.
There's something awry in how you're calling that query, so that would help. Add your connection call with the passwords redacted, if applicable.
|
0

You can also set your variable format to same format used in the database.

define('DATETIME_SQL', 'Y-m-d H:i:s');
$current_time = date(DATETIME_SQL);
$sql = "SELECT member_id, course_id, FROM ".TABLE_PREFIX."vbc_status WHERE end_date < $current_time";

Comments

0

FOUND IT!!!

In $sql = "SELECT member_id, course_id, FROM ".TABLE_PREFIX."vbc_status WHERE end_date < NOW()"; there is an unnecessary , after course_id.

Stupid syntax.... :)

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.