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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:52:05+00:00 2026-06-15T03:52:05+00:00

I have the following code: $sth = $dbh->prepare(SELECT * FROM stats WHERE player_id =

  • 0

I have the following code:

$sth = $dbh->prepare("SELECT * FROM stats WHERE player_id = :player_id AND data_type = :data_type");

$sth->bindParam(':player_id', $player_id);
$sth->bindParam(':data_type', $total_time_data_type_id);
$sth->execute();

$result = $sth->fetch();            

if(!$result){
    $sth = $dbh->prepare("INSERT INTO stats (player_id, offset, created, modified, last_check, data_type, data) VALUES (:player_id, :offset, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), '1', :total_time_data_type_id, '0')");

    $sth->bindParam(':player_id', $player_id);
    $sth->bindParam(':offset', $offset);
    $sth->bindParam(':total_time_data_type_id', $total_time_data_type_id);
    $sth->execute();

    if(!$sth){
        return false;
    }
    $sth = $dbh->prepare("SELECT * FROM stats WHERE player_id = :player_id AND data_type = :data_type");

    $sth->bindParam(':player_id', $player_id);
    $sth->bindParam(':data_type', $total_time_data_type_id);
    $sth->execute();

    $result = $sth->fetch();
    if(!$result){
        return false;
    }
}else{
    $sth = $dbh->prepare("UPDATE stats SET .....");
    //Do more stuff
}

Now, occasionally this is creating duplicate rows (out of ~600 rows, there are 23 duplicates). This confuses me since before inserting the row I explicitly check for a row with the same player_id and data_type.

Multiple rows can exist for either the same player_id or data_type, but not the same of each.

I.E. This would be valid:

ID | PLAYER_ID | DATA_TYPE
---|-----------|----------
1  | 15        | 7
2  | 15        | 18
3  | 92        | 7
4  | 115       | 23

While this would not:

ID | PLAYER_ID | DATA_TYPE
---|-----------|----------
1  | 15        | 7        
2  | 32        | 18       
3  | 15        | 7        
4  | 115       | 23

Because of this I can’t simply declare the player_id field as unique.

The only thing I can think of that may be causing this problem is the fact that the code snippet above is inside of a foreach loop averaging around 115 iterations, and that this code is called again within seconds. Is there a way to prevent this programmatically?

  • 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-15T03:52:06+00:00Added an answer on June 15, 2026 at 3:52 am

    Thanks to everyone for your help, especially @nikita2206. This is how I solved my problem:

    1. Dropped column ID which was Primary Key.
    2. Created new primary key on (Player_id, data_type).
    3. Changed INSERT Statement:

    From

    $sth = $dbh->prepare("INSERT INTO stats (player_id, offset, created, modified, last_check, data_type, data) VALUES (:player_id, :offset, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), '1', :total_time_data_type_id, '0')");
    

    to

    $sth = $dbh->prepare("INSERT INTO stats (player_id, offset, created, modified, last_check, data_type, data) VALUES (:player_id, :offset, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), '1', :total_time_data_type_id, '0') ON DUPLICATE KEY UPDATE player_id = player_id");
    

    Tested by calling the code several times very quickly and no duplicates were created.

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

Sidebar

Related Questions

I have following code.. $query = SELECT quote, author FROM quotes ORDER BY id
I have following code var res = from c in model.Data select new object[]
I have following code for loading image from url in xml parsing endElement method
I have following code in a html form <select name=category class=input onchange=ShowTB(this,'suggest');> <option value=0
I have following code import imp from my_module import MyClass import my_module imp.reload(my_module) print(MyClass
I have following code import sys from ctypes import * from ctypes.util import find_library
I have the following code which should remove all HTML from a part of
i have following code in which, i am fetching the data from the sqlite
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>

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.