how to search using php and sql server as database – PHP

  php

Q(Question):

how does one search using date "from to" while still selecting a user from a drop down list

A(Answer):

Hey.

You should be able to just do something like:

SELECT [stuff]
FROM [tablename]
WHERE [datefield]
BETWEEN '2010-01-01' AND '2010-02-01';

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.

A(Answer):

In addition SQL Server does not have a DATE data type, only DATETIME so to capture a complete two days you will need

SELECT [stuff]
FROM [tablename]
WHERE [datefield]
BETWEEN
'2010-01-01 00:00:00.000' AND '2010-02-01 23.59.59.999';

A(Answer):

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

@code green

That would be two months, would it not?
It would be in MySQL, but I don’t really know SQL Server.

A(Answer):

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

A(Answer):

this is the sample 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

A(Answer):

I prefer to wrap queries in double quotes.
It looks cleaner than escaping, so makes spotting errors easier

$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.

AND ua_startdate >= '$vStartDate' AND ua_startdate <= '$vToDate'

But as Atli suggested, BETWEEN is simpler

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

A(Answer):

thanx a million guyz

LEAVE A COMMENT