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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:14:45+00:00 2026-06-03T04:14:45+00:00

i want to combine a SELECT and UPDATE query, to avoid duplicat select of

  • 0

i want to combine a SELECT and UPDATE query, to avoid duplicat select of rows.

Here is my example code:

private function getNewRCode() {

    $getrcodesql = "SELECT * FROM `{$this->mysqlprefix}codes` WHERE `used` = 0 LIMIT 1;";
    $getrcodequery = $this->mysqlconn->query($getrcodesql);

    if(@$getrcodequery->num_rows > 0){

        $rcode = $getrcodequery->fetch_array();

        $updatercodesql = "UPDATE `{$this->mysqlprefix}codes` SET `used` =  '1' WHERE `id` = {$rcode['id']};";
        $this->mysqlconn->query($updatercodesql);

        $updateusersql = "UPDATE `{$this->mysqlprefix}users` SET `used_codes` =  `used_codes`+1, `last_code` =  '{$rcode['code']}', `last_code_date` =  NOW() WHERE `uid` = {$this->uid};";
        $this->mysqlconn->query($updateusersql);

        $output = array('code' => $rcode['code'],
                        'time' => time() + 60*60*$this->houroffset,
                        'now' => time()
                        );

        return $output;

    }

}

I would like to execute $getrcodesql and $updatercodesql at once, to avoid that the same code is used for different users.

I hope you understand my problem and know a solution for this.

Greetings,
Frederick

  • 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-03T04:14:46+00:00Added an answer on June 3, 2026 at 4:14 am

    It’s easier if you do it the other way round.
    The point is that that your client can generate a unique value before you do the UPDATE and SELECT.

    Change the type of your used column to something else, so that you can store a GUID or a timestamp in it, and not just 0 and 1.
    (I’m not a PHP/MySQL expert, so you probably know better than me what exactly to use)

    Then you can do this (in pseudocode):

    // create unique GUID (I don't know how to do this in PHP, but you probably do)
    $guid = Create_Guid_In_PHP();
    
    // update one row and set the GUID that you just created
    update codes
    set used = '$guid'
    where id in
    (
        select id 
        from codes
        where used = ''
        limit 1
    );
    
    // now you can be sure that no one else selected the row with "your" GUID
    select *
    from codes
    where used = '$guid'
    
    // do your stuff with the selected row
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to combine the following queries into a single query: SELECT val FROM
I want to combine to tables: the sql is: select name, extract(DAY from fechaNacimiento)
I have to select requests that i want to combine using UNION : Table
I want to combine Select top 2 scouting.* From scouting Where scouting.astroLoc Like 'D01%'
I want to combine the following two statements: select password from sys.user$ where name='SYSMAN';
If I want to combine asp.net code with html (mark up) file, I need
I am wanting to update all the rows in the RESULTSET from the SELECT
I want to use different queries such as update, select and etc. Is it
I want to combine the three arrays shown below and I don't know how
I want to combine three tables - date, lead and click - in a

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.