How to search records by date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • goodonya
    New Member
    • Nov 2010
    • 3

    #1

    How to search records by date

    Hi

    I am trying to build a search function on php its working fine but as there are lot of records on database from past it runs very slow. is there way to make it so it only looks up from today's date to future dates nothing in past search term i am using is here

    Code:
    $query = "SELECT * FROM diary WHERE $searchterm LIKE '%$search%' LIMIT 0, 50 ";
    $results = mysql_query( $query );
    Last edited by NeoPa; Nov 2 '10, 05:37 PM. Reason: Please use the [code] tags provided.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    As this is a MySQL question (not PHP related I'm afraid) I will move it across.

    The simple answer is that the WHERE clause should handle that quite easily for you, but I'm not very familiar with that dialect of SQL so I cannot help further.

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      goodonya,

      You need to insert a condition in your WHERE clause, which checks against today's date.

      Using your code as a basis, it should look something like this:
      Code:
      $query = "SELECT * FROM diary WHERE ($searchterm LIKE '%$search%') AND (diary.dateField >= CURDATE()) LIMIT 0, 50 "; 
      $results = mysql_query( $query );

      Comment

      • goodonya
        New Member
        • Nov 2010
        • 3

        #4
        Hi Oralloy

        thank you for replay. I copied code that you put at top and doesn't work for some reason for me it just fails and comes with login screen to database in small window instead of result to search. do you something that i have put wrong ?

        Raj

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          good afternoon goodonya,

          I thought you'd just comment out the line where you build the query and insert mine in its place.

          If you go back and re-run your code, does it behave like it used to? I'm asking, because I can't think of a single reason why simply changing the qurey would require you to login again.

          Anyway, give it a quick try and tell me what you find out. I'm about to leave for three days, so let's get it solved as quick as we can.

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            goodonya,

            I wasn't thinking. Did you replace the string "dateField" in my query with the name of the date field from the table?

            Cheers!

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32669

              #7
              Originally posted by Goodonya
              Goodonya:
              do you something that i have put wrong ?
              Essentially, we can't answer this as we can't see what you've done. You need to post what you have used so that we have something to work with. I say we, but in reality Oralloy is taking the brunt of this. He cannot help you easily unless you post what you're doing.

              Comment

              • goodonya
                New Member
                • Nov 2010
                • 3

                #8
                Hey Guys

                First of all sorry for taking long to get back.

                Oralloy - Thank you very much for your help it was me being dumb as I mentioned I am new to php but i didnt change "dateField" to my database date table and thats what was creating the problem. so all sorted now all working fine so once again thank you for your help.

                NeoPa - I am sorry but this is my first time on Fourms so not sure how it works so just trying to get head round but should keep your tips in mind for next questions and try to provide full details so can be easier for person to answer. Thank you

                Cheers

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32669

                  #9
                  That's a fine attitude Goodonya, but remember you will benefit as much, if not more so, than we will when you provide the required information (in your future questions). What's good for the goose is good for the gander ;-)

                  Comment

                  • Oralloy
                    Recognized Expert Contributor
                    • Jun 2010
                    • 988

                    #10
                    goodonya,

                    I'm glad you got your problem sorted out.

                    Good luck with the rest of the project!

                    Comment

                    Working...