Can 2 actions happen simultaneously in PHP / MySql?

by Chris-
6 replies
Hi all,

I am a newbie at PHP, and am considering using PHP with MySql to give each visitor who signs-up a unique ID-number. I plan to do this by having a single ID-number variable in a MySql database, then each time a visitor signs-up, increment the ID-number variable by one, store their email with this new unique number, (and email the ID-number to them).

I am hoping for lots of traffic, so was wondering if it is ever possible for two visitors to submit the same form at exactly the same time and thus run two actions on the MySql database at the same time, which would result in 2 different visitors having exactly the same ID-number, which, obviously, I don't want.

I am assuming that the actions done on a single MySql database can only do a single sequence of actions (increment variable, read changed variable, store variable and email together, finish) at any time, so there can be no possibility of my programming idea ever resulting in 2 identical ID-numbers, but I thought I'd better make 100% sure before assuming my guess is correct So I guess I am asking, does a sequence of actions get done as a single continuous set before anything else can access the same database, or is it possible for two such sequences to get interleaved so both sequences increment the variable before either has read it? Or if either is possible, how to ensure that a particular action-sequence gets completed before another action-sequence is allowed to start???

thanks in advance for the answer to this

Chris
#actions #happen #mysql #php #simultaneously
  • Profile picture of the author IdeaBox
    a MySQL server alone can handle upwards to 2000 connections without a problem. If you're connection rate higher then this I would suggest pooling. However, just because the MySQL server can handle 2000 connections doesn't mean your host can. If you're using shared hosting you are probably just 1 of 20-30 domains running from that server. You'll have to take in account their traffic as well.
    {{ DiscussionBoard.errors[8590604].message }}
  • Profile picture of the author Chris-
    Thanks for the info. Are you saying that 2000 actions can all be done simultaneously, so there is no way to ever increment a variable in MySql and read it before it can be incremented again by PHP on a different webpage?

    Chris
    Signature
    Unusual opportunity! CPA type offer (no selling), commissions $1k to $300k each time, in-person (US), phone or online. Click here for more details.
    {{ DiscussionBoard.errors[8590610].message }}
  • Profile picture of the author Voon
    I think you no need to worry about having same ID generated from mysql, it will handle the concurrent access nicely for you, as this is what mysql is built for.

    If you want to know, inside mysql it will lock the table/row(depend on the database engine) while performing insert, update, delete operation until it is done. So the second operation will perform once the table is unlocked, and this happen automatically in split seconds. Unless your site is horribly overloaded, or operations on many thousands of records, you won't even notice the wait. You can dig up manual if you want to here.

    As for connections, that is another issue, but you can read up here.
    Signature

    .

    {{ DiscussionBoard.errors[8590844].message }}
  • Profile picture of the author Chris-
    Thanks very much, Voon, for giving me the answer I was hoping for

    Chris
    Signature
    Unusual opportunity! CPA type offer (no selling), commissions $1k to $300k each time, in-person (US), phone or online. Click here for more details.
    {{ DiscussionBoard.errors[8590856].message }}
  • Profile picture of the author SteveJohnson
    I am assuming that the actions done on a single MySql database can only do a single sequence of actions (increment variable, read changed variable, store variable and email together, finish)
    The proper way to do this would be to us MySQL's autoincrement feature. You let the db worry about incrementing the ID of the record, which it will do on each INSERT. MySQL will return the value of the incremented field on successful insertion.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[8593082].message }}
  • Profile picture of the author Chris-
    Even better, thanks Steve

    Chris
    Signature
    Unusual opportunity! CPA type offer (no selling), commissions $1k to $300k each time, in-person (US), phone or online. Click here for more details.
    {{ DiscussionBoard.errors[8593087].message }}

Trending Topics