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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:13:51+00:00 2026-06-02T17:13:51+00:00

I very frequently use logic something like this when writing to normalised databases. In

  • 0

I very frequently use logic something like this when writing to normalised databases.

In pseudocode:

is the thing I want in the table?:
    yes - get it's ID
else
    no - insert it, then get it's ID

In PHP:

// is the useragent in the useragent table?
// if so, find the id, else, insert and find.
$useragentResult = $mysqli->query("SELECT id FROM useragent WHERE name = '".$useragent."' LIMIT 1");
if ($useragentResult->num_rows == 0) {
    // It is not in there
    $mysqli->query("INSERT INTO useragent (name) VALUES ('".$useragent."')");

    $resultID_object = $mysqli->query("SELECT LAST_INSERT_ID() as id");
    $row = $resultID_object->fetch_object();
    $useragentID = $row->id;
} else {
    // It is, so find it and set it
    $useragentData = $useragentResult->fetch_object();
    $useragentID = $useragentData->id;
}

This feels ugly (not just due to PHP!), and common enough that perhaps there is a better way.

What’s the real way of doing this, or is this the best way?

  • 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-02T17:13:52+00:00Added an answer on June 2, 2026 at 5:13 pm

    Use INSERT ... ON DUPLICATE KEY UPDATE.

    Since MySQL 5.5:

    If a table contains an AUTO_INCREMENT column and INSERT ... ON DUPLICATE KEY UPDATE inserts or updates a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value.

    Or in earlier versions:

    If a table contains an AUTO_INCREMENT column and INSERT ... ON DUPLICATE KEY UPDATE inserts a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value. If the statement updates a row instead, LAST_INSERT_ID() is not meaningful. However, you can work around this by using LAST_INSERT_ID(expr). Suppose that id is the AUTO_INCREMENT column. To make LAST_INSERT_ID() meaningful for updates, insert rows as follows:

    INSERT INTO table (a,b,c) VALUES (1,2,3)
      ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server I frequently use FOR XML AUTO, which allows something like SELECT
Very quick question - I'm writing a basic script which updates a table based
I have a MySQL table that's being updated very frequently. In essence, I'm trying
I'm familiar with and use very frequently the C-l ( recenter-top-bottom ) to Move
I have a program i frequently use that is made with .NET. This program
This pattern comes up very frequently in my code: x= x== 0? 1: x;
springframework.beans.BeanUtils is very useful to copy objects, and I use the ignoreProperties option frequently.
I need to perform this SQL very frequently UPDATE Users SET UserPoint=UserPoint+(@UserPoint) WHERE UserID=@UserID
I have got a project, where I have to do this very frequently: if
I find myself very frequently wanting to write reusable strings with parameter placeholders in

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.