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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:19:24+00:00 2026-05-27T14:19:24+00:00

How do I lock mysql tables in php? I currently have this code: $db->query(LOCK

  • 0

How do I lock mysql tables in php? I currently have this code:

$db->query("LOCK TABLES tbl_othercharge WRITE");
for($x=0;$x<=500; $x++){
    $id = get_max();
    $db->query("INSERT INTO tbl_othercharge SET tblocID = '$id', assessmentID='lock1'");
}

$db->query("UNLOCK TABLES");

Here’s the get_max() function, which obviously will fail if the script above executes simultaneously.

 <?php
    function get_max(){
        global $db;
        $max = $db->get_var("SELECT MAX(tblocNumber) FROM tbl_othercharge");
        if($max == null){
            $max = 1;
        }else if($max >= 1){
            $max = $max + 1;
        }
        return 'OC'.$max;
    }
    ?>

I’m trying to test if there are still concurrency problems by executing the same script on 2 browsers.
The script above inserts 400+ records instead of 999 records. How do I properly lock the table while I’m inserting something into it.

I want to lock the table to prevent something like this to happen:
enter image description here

As you can see the field with the prefix ‘OC’ on it should have a number which is equal to the auto-increment primary key.

  • 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-27T14:19:25+00:00Added an answer on May 27, 2026 at 2:19 pm

    The only reliable solution is to do an insert with a dummy value, getting the last insert id, and updating the row to the correct value.

    mysql_query("INSERT INTO table (field) VALUES (dummy);");
    $id = mysql_last_insert_id();
    mysql_query("UPDATE table SET field='OC{$id}' WHERE id={$id} LIMIT 1;");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I call update/insert into table(tb1) after calling: LOCK TABLES tb1 READ ? I've
I have this query: UPDATE table_name SET field_1 = field_1 +100, field_2 = field_2
I have a MySQL ISAM table being accessed my multiple php instances. Right now
Is there a way to lock a Mysql table only for write, so that
I am trying to write a PHP program to automatically created backups of MySQL
Handling Multi Users Requirements: I have an applications (mysql php jquery) where the users
I have a really bad time with a query on MySQL 5.1. I simplified
How can I look all tables in a MySQL database in PHP? Im sure
I have a PHP/5.2 driven application that uses transactions under MySQL/5.1 so it can
I have an online game that is PHP & MySQL based (PHP 5.2.9 &

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.