Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP where clause

  • 13-09-2006 02:40PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 68,173 ✭✭✭✭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, Registered Users 2 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, Registered Users 2 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