On Wed, 2010-02-03 at 00:17 +0100, Rene Veerman wrote: > and after the sleep(rand(1,3)) it might need a short loop like this; > $rnd = rand(1,99999); $a=0; > for ($i=0; $i<$rnd; $i++) { $a++ } > > to further randomize the retry attempt.. > > > On Wed, Feb 3, 2010 at 12:05 AM, Rene Veerman <rene7705@xxxxxxxxx> wrote: > > > i haven't had the pleasure yet of writing for sites that generate so many > > hits/sec that > > they'd update the max value of any table at exactly the same time. > > > > i usually ask for the max value about 2 milliseconds before doing the > > insert. > > And if the insert fails, i can auto-retry via a wrapper function after > > sleep(rand(1,3)); > > I dare say i could work this way at facebook <g> (not that i really want > > to, happy with where i am) > > > > > > On Tue, Feb 2, 2010 at 11:46 PM, Ashley Sheridan <ash@xxxxxxxxxxxxxxxxxxxx > > > wrote: > > > >> On Tue, 2010-02-02 at 23:19 +0100, Rene Veerman wrote: > >> > >> function getMax($table, $field) > >> > >> > >> If I saw this sort of code I'd be appalled! It's possibly the worst way to > >> get the auto increment value. You won't notice it testing the site out on > >> your own, but all hell will break loose when you start getting a lot of > >> hits, and two people cause an auto increment at the same time! > >> > >> Thanks, > >> Ash > >> http://www.ashleysheridan.co.uk > >> > >> > >> > > The problem is where 2 people choose the same instant to perform an action on your site that inserts a record into your db. The db engine inserts them one after the other, and then responds about the max(id) to your PHP script. Then, you now have 2 people who have the same max(id) retrieved, but one of the values is wrong. Thanks, Ash http://www.ashleysheridan.co.uk