...

View Full Version : Resolved Don't Add Duplicate Time for Post



mlmorg
02-20-2009, 10:51 PM
I created a blog with an admin section and my friend wants to be able to change the published date/time of a post (it had formerly just added the time depending on when he published it).

This is fine, but I NEED the time to be distinct in the database. How can I run a query that checks if the time I'm about to put in the DB is already in there and, if it is, adds 1 second to the time until I get a distinct time?

Thanks for any help!


Can it be as simple as this:



<?php
while(!isset($time_success)){
$query = "SELECT * FROM posts WHERE time = '$time'";
$result = mysql_query($query);
if(mysql_num_rows($result)){
$time = $time + 1;
} else {
$time_success = 'success';
}
}
?>

Fumigator
02-20-2009, 11:31 PM
The strategy you included will work fine, maybe a little tweaking on incrementing the $time variable, but generally pseudo-codally that works.

I would probably make sure my publish datetime value is an actual MySQL DateTime data type, which includes microseconds, so the possibility of a duplicate happening with that kind of precision is extremely remote. If the admin only enters date, hour and minute, you can randomize the seconds and microseconds to bring the chances you'll have a duplicate down to around .0000000001%.

And of course, you'll eventually see a duplicate so just make sure the datetime has a unique index on it so the query will fail if it is a duplicate.

mlmorg
02-21-2009, 01:03 AM
Ok cool thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum