how to search using php and sql server as database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ewwachi
    New Member
    • Feb 2010
    • 11

    how to search using php and sql server as database

    how does one search using date "from to" while still selecting a user from a drop down list
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    You should be able to just do something like:
    [code=sql]SELECT [stuff]
    FROM [tablename]
    WHERE [datefield]
    BETWEEN '2010-01-01' AND '2010-02-01';[/code]
    I'm no expert on Microsoft SQL Server in particular, but the basic syntax is the same for most types of SQL based databases.


    If that is not what you are looking for, please elaborate on your question. Perhaps show us a sample of the code you are having trouble with.

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      In addition SQL Server does not have a DATE data type, only DATETIME so to capture a complete two days you will need
      Code:
      SELECT [stuff] 
      FROM [tablename] 
      WHERE [datefield] 
          BETWEEN 
      '2010-01-01 00:00:00.000' AND '2010-02-01 23.59.59.999';

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        It doesn't? Seems like somewhat of an oversight, to be honest.

        ... barely managed to refrain from taking a cheap-shot at M$ just now xD

        Originally posted by code green
        to capture a complete two days
        That would be two months, would it not?
        It would be in MySQL, but I don't really know SQL Server.

        Comment

        • code green
          Recognized Expert Top Contributor
          • Mar 2007
          • 1726

          #5
          Sorry, yes two months.
          Dates are a pain. Have to use CAST or CONVERT to strip off time segment.
          Need to be careful with comparisons, which is why I rather rudely interupted, Atli

          Comment

          • ewwachi
            New Member
            • Feb 2010
            • 11

            #6
            this is the sample code

            Code:
            $vUserActQuery = '	SELECT ds_description,ua_busscompid, ua_type, ua_actionid , ua_startdate, ua_enddate, user_comments 
            								FROM dms_useraction,status
            								WHERE ua_userid = \''.$vUserId.'\' AND ua_actionid=ds_id' from this part its wrong help me out AND ua_startdate >= \''.$vStartDate.'\' AND ua_startdate <= \''.$vToDate.'\';
            thnx
            Last edited by Atli; Feb 3 '10, 12:39 PM. Reason: Added [code] tags.

            Comment

            • code green
              Recognized Expert Top Contributor
              • Mar 2007
              • 1726

              #7
              I prefer to wrap queries in double quotes.
              It looks cleaner than escaping, so makes spotting errors easier
              Code:
              $vUserActQuery = "SELECT ds_description, ua_busscompid, 
              ua_type, ua_actionid , ua_startdate, ua_enddate, user_comments 
              FROM dms_useraction,status
              WHERE ua_userid = $vUserId AND ua_actionid = ds_id
              from this part its wrong help me out 
              AND ua_startdate >= $vStartDate AND ua_startdate <= $vToDate;
              If I have cleaned this correctly, I can see straight away there are no single quotes around the dates.
              I used to make that error.
              Code:
              AND ua_startdate >= '$vStartDate' AND ua_startdate <= '$vToDate'
              But as Atli suggested, BETWEEN is simpler
              Code:
              AND ua_startdate BETWEEN '$vStartDate' AND '$vToDate'
              If $vUserId is a string that needs wrapping in quotes.
              DON'T overlook the time element as in my first post.
              And may I suggest studying the JOIN command

              Comment

              • ewwachi
                New Member
                • Feb 2010
                • 11

                #8
                thanx a million guyz

                Comment

                Working...