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 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    checking for a week range

    Hi everyone,

    I have a query to check the data for a week starting from the date entered by the user, but it's not working. I would be very grateful if you can have a look.

    PHP Code:
    function GetDateSelectString($IsPosted true
         { 
        return  
    $_POST['sd_year'] . '-' $_POST['sd_month'] . '-' $_POST['sd_day']; 
         } 

    $PostedDate GetDateSelectString(); 

    $query1 = ("SELECT m_room_details.room_code, m_room_details.room_number, room_name, m_session.session_id, session_date, session_start_time, session_end_time, room_cost_per_session, booking_indicator FROM m_room, m_room_details, m_room_session, m_session WHERE m_room_details.room_code = m_room_session.room_code AND m_room_details.room_number = m_room_session.room_number AND m_room.room_code = m_room_details.room_code AND booking_indicator = 'YES' AND m_room_session.session_id = m_session.session_id AND session_date BETWEEN ('".$PostedDate."' AND ('".$PostedDate."' + INTERVAL 7 DAY ))  "); 
                         
    $result1 mysql_query($query1) or die('Query failed miserably: ' mysql_error()); 
    $numfields mysql_num_fields($result1); 
    the query works fine if i remove the condition
    PHP Code:
    AND session_date BETWEEN ('".$PostedDate."' AND ('".$PostedDate."' INTERVAL 7 DAY )) 
    :

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The "INTERVAL 7 DAY" needs to be part of the date_add() function. You can also use "1 WEEK".

    PHP Code:
    AND session_date BETWEEN ('".$PostedDate."' AND date_add('".$PostedDate."'INTERVAL 1 WEEK )) 

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks but it still doesn't work. the query works only when I remove the date part. By the way I am echoing the $PostedDate and it's showing the right selected date in the right format

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    "It doesn't work" isn't helpful. "I got a sql syntax error and here it is" or "No rows are returned" is helpful.

    The code I posted needed a tweak, the parenthesis were askew.
    PHP Code:
    AND (session_date BETWEEN '".$PostedDate."' AND date_add('".$PostedDate."'INTERVAL 1 WEEK )) 
    I just ran this on a table of mine and it works fine.

  • #5
    New Coder
    Join Date
    Aug 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that works thanks

    sorry i didn't post the error message, it was just a normal sql error "you have an error in your syntax....."

    Many thanks.


  •  

    Posting Permissions

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