Forum Moderators: open

Message Too Old, No Replies

problem with simple query using DATETIME comparison

comparison using mysql DATETIME is returning record wit non-perfect match

         

sunswept

8:23 am on Aug 3, 2007 (gmt 0)

10+ Year Member



I have a db table named 'people' with the primary key userId that is generated with autoincrement. The table has a column dateTimeCreation (a mysql DATETIME type) whose value is set in the php script using

$dateTimeCreation = date ( 'Y-m-d h:m:s' );

and inserted in the record creation query along with several other values.
To get the userId of the new record immediately after creation i execute a query

"SELECT userId from people where dateTimeCreation = STR_TO_DATE('{$dateTimeCreation}', '%Y-%m-%d %h:%i:%s');";

Unexpectedly the query sometimes returns records with dateTimeCreation values that arent perfect matches. It has returned records with year/month/date and hour matching and minute and second not matching, this when a record with perfectly matching dateTimeCreation value existed lower in the table (lower as seen in phpmyadmin gui).
This is one of the nasty problems that occur only sometimes. But i checked my logs and incorrect matches were definitely retreived.
I am aware that there are better techniques to get the new records as they are created, but some parts of my code still depend on queries using dateTimeCreation so i have to make this comparison work.
Any suggestions?

venelin13

10:17 am on Aug 3, 2007 (gmt 0)

10+ Year Member



Why you just do not use:


"SELECT userId from people where dateTimeCreation = '$dateTimeCreation' ";

sunswept

11:25 am on Aug 3, 2007 (gmt 0)

10+ Year Member



vanelin
I dont think that will work either
That is what i was doing earlier, when the problem (as described above) cropped up i thought it could be mysql comparing its datetime type with a string value so i typecast it to datetime format. But the problem persisits.
Thanks for trying.
Anyone with more suggestions?

Duskrider

2:18 pm on Aug 3, 2007 (gmt 0)

10+ Year Member



You could try moving both dates into a unix timestamp format and comparing that way.

$dateTimeCreation = strtotime($dateTimeCreation);
"SELECT userId from people where UNIX_TIMESTAMP(dateTimeCreation) = '$dateTimeCreation'"

Then if it still doesn't work, you can echo both values to see how many seconds they're off of each other and narrow down the problem.

sunswept

3:31 pm on Aug 3, 2007 (gmt 0)

10+ Year Member



thanks duskrider
I could try that but like i said, the problem doesnt crop up everytime. Even the current code works most of the time. Since i am ubable to recreate the circumstances of failure I cannot be sure if your solution works coorrectly all the time.

I guess i need to find out what can be wrong with the current code, then look for alternatives, otherwise i might find myself trying alternatives for a long time.

Can anyone point out something wrong with the code up there?
or maybe just some thoughts about what else could cause such a problem with a seemingly straightward comparison.