Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Select DATE help

    Hi,

    how would i write a select statement, where i only want to retrieve the records where the "startdate" (held in DB) is less than 7 days old from the current date.

    thanks in adavance.

  • #2
    Regular Coder
    Join Date
    Dec 2005
    Posts
    346
    Thanks
    1
    Thanked 0 Times in 0 Posts
    This will work...just modify it to meet your db & other query requirments.
    PHP Code:
    $sql mysql_query("SELECT * FROM table WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= date_field"); 

  • #3
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i'm getting an error, the code i have is below.

    <?php

    require ('database3.php');

    $sql = mysql_query("SELECT * FROM monksc.news WHERE DATE_SUB(CURDATE(),INTERVAL 14 DAY) <= startDate");

    $result = mysql_query ($sql) or die (mysql_error());

    if (mysql_num_rows($result)>0)

    {

    while($row=mysql_fetch_array($result))

    {

    print $row['id'];

    }

    }

    ?>

  • #4
    Regular Coder
    Join Date
    Dec 2005
    Posts
    346
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kal
    i'm getting an error, the code i have is below.

    <?php

    require ('database3.php');

    $sql = mysql_query("SELECT * FROM monksc.news WHERE DATE_SUB(CURDATE(),INTERVAL 14 DAY) <= startDate");

    $result = mysql_query ($sql) or die (mysql_error());

    if (mysql_num_rows($result)>0)

    {

    while($row=mysql_fetch_array($result))

    {

    print $row['id'];

    }

    }

    ?>
    What is your error?

  • #5
    New Coder
    Join Date
    Nov 2002
    Location
    Wien Österreich//Vienna Austria
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i'm getting an error, the code i have is below.
    which error?

  • #6
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1

  • #7
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i've sorted it now, thanks for your help. i had "mysql_query" written before the select statement

  • #8
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    It always helps to actually list the error you get when asking for help.


  •  

    Posting Permissions

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