Martin van den Berg wrote:
I have this piece of php-code which inserts data into a database.
Before inserting it must verify if the data is unique. The php code
looks something like:

$query = "SELECT id FROM mytable WHERE bla LIKE " . $x .";
$rows = execute( $query )
if ( $rows == 0 )
{
   /* some more processing */
  $query = "INSERT INTO mytable ...... etc etc
  execute( $query )
}

Now here is the problem: when the user enters the page, and directly
refreshes the record is inserted twice.... Is is possible that both
requests are processed simulatiounsly by the server (apache on linux)?
And can I add something like a critical section or semaphore to
overcome this problem.

Thanx,

Martin

It's my understanding that a SELECT has a higher priority than an INSERT on most MySQL setups.

I've ran into this problem on a site I hobby-code on also. It's my guess that the write isn't hitting the DB in time for the second read to pick it up, but that's a guess. As far as what to do about it if that's the problem, I'll let someone else come up with that answer. ;) It's not *that* critical in my app and it doesn't happen often enough to be a real problem for me, but I would like to know if there's a way around it.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to