Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP where clause

Options
  • 13-09-2006 2:40pm
    #1
    Registered Users Posts: 673 ✭✭✭


    Hey,

    Im using this bloody php where clause in my mysql query but it isint doing what i expect it to.
    WHERE order_date >= ".$search_date_from." AND order_date <= ".$search_date_to."
    

    This should be easy peesey so i am freaking out here. This is valid syntax isint it?

    Thanks


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    A bit of info on what your values are? What error are you getting? And you're better off giving us the whole statement because the end of that statement should be

    [php]WHERE order_date >= ".$search_date_from." AND order_date <= ".$search_date_to."";[/php]


  • Closed Accounts Posts: 169 ✭✭akari no ryu


    It's also not PHP, it's sql of some description.
    Can you echo out the resulting sql and paste that in too, please.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    At a guess....

    You havea field order_date in your MySQL database which is a datetime field (i.e. of the format YYYY-MM-DD HH:MM:SS), and you're using a standard date, e.g 01-02-2005, in the $search_date_from and $search_date_to variables.

    Your best bets, to get it working the way you want, is to reformat all dates in to the YYYY-MM-DD HH:MM:SS format (or whichever way it's stored in the DB), and use single quotes around your values, i.e.
    [php]
    $query .= "WHERE order_date >= '".$search_date_from."' AND order_date <= '".$search_date_to."'";
    [/php]

    Don't forget to escape your variables!

    </shot_in_dark>


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Here's the whole query:
    $query = "SELECT *
    		FROM my_table
    		WHERE order_date >= ".$search_date_from." AND order_date <= ".$search_date_to."
    		ORDER BY order_date DESC, order_time DESC
    		";
    
    		$results = mysql_query($query)
    			or die(mysql_error());
    
    		while ($rows = mysql_fetch_array($results)) {
    		extract ($rows);
    

    All the variables are working correctly. If i just use the
    order_date >= ".$search_date_from."
    
    in the query without the
    AND order_date <= ".$search_date_to."
    
    it works fine also so im just wondering if my syntax in this line is correct?

    If i echo out the date variables they display as expected. Im really just trying to diplay the results from my database that lie between two seperate dates. I have transfered the dates to the correct format for the database also so thats not the issue.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Ok try something like this:

    [php]$SQL ="SELECT * FROM your_table WHERE order_date BETWEEN '$search_date_from' AND '$search_date_to'";[/php]


  • Advertisement
  • Registered Users Posts: 673 ✭✭✭Bananna man


    Ok try something like this:

    [php]$SQL ="SELECT * FROM your_table WHERE order_date BETWEEN '$search_date_from' AND '$search_date_to'";[/php]

    Thats doing it now alright, cheers. Still at a loss as to what i was doing wrong before though!!


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    No problem! I don't know where the problem lay but I had a similar string to yours originally for what I've just given you and couldn't get it to work either, thats how I ended up using BETWEEN.


Advertisement