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 8132451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:21:14+00:00 2026-06-06T09:21:14+00:00

I have a security problem. I think to lock a table would be a

  • 0

I have a security problem. I think to lock a table would be a solution, only I don’t know how to properly do it in MySQL, PHP, and don’t have enough time to search Google and documentations anymore.

So, the task:
Make a database and write a php code to randomly get lottery-tickets.
I have a table, that contains only a few row. (id, name, quantity)

So for example:

1 – no win – 20000
2 – Hello Kitty bag – 200
3 – a very nice pen – 50
etc.

I wrote the php, so the logic’s the following:
1) Get the amount of evey tickets from the database, and create a PHP array that contains the intervals, like:
no win: 20000,
Hello Kitty bag: 20200,
a very nice pen: 20250.
2)Generate a random number from 1 to max, so I know what ticket the user got.
3)Update the database: subtract 1 from the proper row.

Now, this works great, however the problem:
What if I have a very large amount of users, and two or three of them clicks at the same time, random the same number, (lets assume it’s hello kitty) but only have 1 of that item?
All the three of them subtract from the database, not stopping at 0. (In this example, we would have -2 hello kitty bag)

Huge issue, at least for me.
So in summary, my question is:
1)How can I lock the table from the selection, until I am ready to subtract? Is it a good solution?
2)Can I make it one query, or MySQL can’t handle that?
3)Other solutions?

I appreciate every single answer, really!
Thanks in advance, also sorry for the long post, but wanted to keep things straight.

[ SOLVED ]
I used a stored procedure, here are the details.
I had some luck to have my server version above 5.x, since as I read the MyISAM system supports stored procedures only above this version.
I will write some code here hoping someone’ll find it useful in the future:

delimiter //
CREATE PROCEDURE `name`(IN a_parameter INT(20))
  DECLARE some_text VARCHAR(50) DEFAULT '';
  #now using all this stuff
  SELECT `name` INTO some_text FROM `users_table` WHERE `table_id` = a_parameter;
  SELECT some_text
END//
delimiter ;
  • 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-06-06T09:21:16+00:00Added an answer on June 6, 2026 at 9:21 am

    You need to use a transaction (documentation here), and a table lock (read about the interaction between table lock and transactions here).

    To do it in one query you can build a stored procedure (documentation here). I will go with a procedure.

    If you are using PHP PDO you can read all about in in the manual (here, most important PDO:: beginTransaction and PDO:: commit). If you are still using mysql extendion you need to do it explicit like this:

    mysql_query('START TRANSACTION');
    // Because you cannot nest lock's and transactions, this is a workaround to lock the table
    mysql_query('SELECT * FROM foo FOR UPDATE'); // Lock issued, the important part is "FOR UPDATE"
    //[ your query's here ]
    
    // if something goes wront, revert the changes
    mysql_query('ROLLBACK');
    // at the end commit the changes
    mysql_query('COMMIT');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing, non-framework-based PHP/MySQL website. It has a simple security model, with
I would like to know if embed js is a problem for site security,
I have the following problem with the timeouts in Spring Security with JSF: I've
I have a security consultant demanding that we implement encrypted connections to mySQL. He
I have a security number generator device, small enough to go on a key-ring,
I have what I think is a bizarre problem. With the same mapping to
I have a problem applying css to the web pages, using spring security (3.0.7
Have next problem: i setup the custom session state server (store sessions in Mysql).
I have a problem. I would appreciate if you can guide me. I have
I have problem with querying webservice which uses WS-Security. I use code creadted by

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.