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

  • Home
  • SEARCH
  • 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 8535047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:20:20+00:00 2026-06-11T10:20:20+00:00

I have a table with this columns : [user_id][game_id] I need to close the

  • 0

I have a table with this columns : [user_id][game_id]
I need to close the game when two player join the game.
I used this code :

if(mysql_num_rows(mysql_query("SELECT user_id FROM live_games WHERE game_id = '$gid'"))<2){
mysql_query("INSERT INTO live_games (user_id, game_id) VALUES ('$uid', '$gid')");
echo "You have joined the game";
}else{
echo "Table is full";
}

the code will allow only two player to register but some times when there are too many users in the website , this condition will not work and three user will add in the table.
How can i fix?

  • 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-11T10:20:21+00:00Added an answer on June 11, 2026 at 10:20 am

    You have a few options:

    1. If using the InnoDB storage engine, perform a locking read with SELECT ... FOR UPDATE within the same transaction as the INSERT statement.

      Using PDO:

      $dbh = new PDO("mysql:dbname=$dbname;charset=utf8", $username, $password);
      $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
      
      $dbh->beginTransaction();
      $qry = $dbh->prepare('
        SELECT COUNT(*) FOR UPDATE FROM live_games WHERE game_id = ?
      ');
      $qry->execute([$gid]);
      
      if ($qry->fetchColumn() < 2) {
        $qry = $dbh->prepare('
          INSERT INTO live_games (user_id, game_id) VALUES (?, ?)
        ');
        $qry->execute([$uid, $gid]);
        if ($qry->rowCount() && $dbh->commit()) echo 'You have joined the game';
      } else {
        $dbh->rollBack();
        echo 'Table is full';
      }
      
    2. Change the table structure so that there are two columns, player1 and player2 (initially NULL) and perform UPDATE live_games SET player2 = ? WHERE game_id = ? AND player2 IS NULL, then check the number of affected rows; or

    3. Change the table structure so that there is an additional playerNumber column and then create a composite UNQUE index over (game_id, playerNumber).

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

Sidebar

Related Questions

I have table with two columns (varchar from, varchar to). This table represents connections
i have this table which has around 5 columns in it column1, user_id, column3,
I have a mysql table with the following columns: group_id game_id user_id message last_update
I have this sql statement: SELECT game.game_id FROM game, userGame WHERE userGame.user_id = 1
I have user and user_role tables. The user_role table has two columns: user_id role_name
I have a table TreeStructures wich holds structures of Trees. This table has columns
I have a table and this table contain multiple columns. I want to know
I have a table of product information with many bit columns. This table can
Let's say I have a table Foo . This table has the columns ID
I have a table structure with columns like this [ID] [Name] [ParentId] [ParentName] The

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.