SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help Very Stuck!!!

    hello

    having huge problems with a simple delete but seem to be getting nowhere, bascially what i want to do is delete an entry in a sessions table if its older than such a date, yet not have ny script fall over if there are no entries in the sessions table

    at the moment i am using this

    $too_old = "time() - 50";
    $query = "DELETE FROM sessions WHERE created < '$too_old'";
    $result = mysql_query($query);

    which does nothing yet if i change it to...

    $query = "SELECT created from sessions WHERE sessionid = 1";
    $result = mysql_query($query);
    $answer = mysql_result($result, 0);
    echo $answer;

    it works no problem, so i know my database is fine, also if i put a clause at the end of my "$result = mysql_query($query);" making it

    $result = mysql_query($query) or die("Failed!");

    it returns failed, please help me if you can cos i've been stuck on this since monday and am a complete beginner.

    thanks in advance
    Garrett

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Your problem lies here:

    $too_old = "time() - 50";

    Let's say that time()=800, what do you think is in the variable $too_old? (Hint: it isn't 750.) Answer: time() - 50
    If you are still confused, try echoing out $too_old and see what is displayed. That output is going into your SELECT statement, and since mysql doesn't understand that syntax, the query fails.
    Last edited by 7stud; Jun 6, 2001 at 02:51.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok so echoing that gives me...

    time() - 50

    but i still dont understand, what i want to do is check is the time older than the current time - 50 (which is short for the moment just to test) so how do i do this if what i have is not correct.

    Garrett

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok so same line without quotes! i understand, but my delete still does'nt work probably something about the time formats

    in my created entry in the sessions table is 20010605133358
    echo $too_old; gives me 991829774

    so nothing happens with the following script, it does'nt delete and it does'nt fail so it thinks the entry does'nt correspond, correct??

    $query = "DELETE FROM sessions WHERE created < '$too_old'";
    $result = mysql_query($query) or die("failed");

    Garrett

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    solved it!

    $date = date ("YmdHis");
    $time_out = 6000;
    $too_old = $date - $time_out;

    $query = "DELETE FROM sessions WHERE created < '$too_old'";
    $result = mysql_query($query) or die("delete failed");

    this is what i am using now and works no problem, when i used $too_old = "time() - 50" i had quotes when there should'nt have been any and the time format was the wrong one, so while the code was correct and did'nt create any errors, it did'nt work ether cos the format in the php and in the table were different. I also found an invaluable page on php.net which gives all the date formats for anybody else who needs them

    http://www.php.net/manual/en/function.date.php

    thanks everyone for your help

    Garrett


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •