Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6713949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:26:22+00:00 2026-05-26T08:26:22+00:00

I am making a website in php, in which I want to sell some

  • 0

I am making a website in php, in which I want to sell some charge-numbers. For each request, I have to do these operations atomic:

  • Asking database for an available charge-number
  • marking the charge-number as sold
  • increasing a number (in a file or database) which shows the number of clients got a charge-number

I know how to do these operations and if it may help, I am using mysql. But my problem is how to do these operations atomic between all request? I mean how can I force the web-server(apache) and php interpreter to run this part one by one for all request, not in a parallel manner?

P.s: Please make your answers as a solution by php, not a database related solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:26:23+00:00Added an answer on May 26, 2026 at 8:26 am

    All of these operations are really database-dependent. So you don’t really need to make PHP run code in a critical section; it’s enough to serialize these operations at the database.

    The simplest way to do that would be to LOCK TABLES ... WRITE while you perform these; this guarantees that only one script would be talking to the database at once.

    Another approach would be to SET TRANSACTION ISOLATION LEVEL SERIALIZABLE and run all operations in a transaction with autocommit turned off (you should really use a transaction to ensure data integrity even if you decide to go with the table lock though).

    Update: If you absolutely must do this in PHP, then you can achieve the goal using flock:

    $fp = fopen('sunc_file', 'r+');
    if (flock($fp, LOCK_EX)) {
        // Perform database ops here
        flock($fp, LOCK_UN); // release the lock
    }
    else {
        die("Couldn't get the lock!");
    }
    fclose($fp);
    

    flock in exclusive mode will prevent any other process from locking the guard file and thus let your scripts execute strictly serialized, but please read the giant red warnings on the man page!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a website to sell widgets. I have a table for widgets, each
I have my first professional assignment of making a website in which a photographer's
I'm making my own RSS feed in PHP for my website, which loads the
i'm making a bookmarklet to my website, and for now i have: <a href=javascript:location.href='http://website.com/compartir_link.php?url='+encodeURIComponent(location.href)
I am making a classifieds website... I have these 6 tables: Every category has
I have a little experience in php and I am making a website for
I'm planning on making some dynamic PHP websites and I need a free Authentication
I'm making a website that tend to handle all the request in one page
I am making the website of a movie theater, and I want Google Movies
I'm making a website using JSP and servlets and I have to now break

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.