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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:59:23+00:00 2026-05-13T20:59:23+00:00

On the manual page; http://dev.mysql.com/doc/refman/5.0/en/mysql-insert-id.html It is said that Returns the value generated for

  • 0

On the manual page;
http://dev.mysql.com/doc/refman/5.0/en/mysql-insert-id.html
It is said that “Returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement.”

The problem is that it doesn’t really work for me after an update.

The real need is that I’m trying to update a row in a table (updated with a “WHERE” conditions) and then, if a row indeed was updated, get it’s full details.
So first I UPDATE, then I need it’s id to get it’s details with a SELECT statement.

Here’s an example code which shows the problem:

#define _WIN32_WINNT 0x0400
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <stdio.h>
#include <conio.h>

#include <winsock.h>

#include "C:/Program Files (x86)/MySQL/MySQL Server 5.0/include/mysql.h"
#pragma comment(lib, "C:/Program Files (x86)/MySQL/MySQL Server 5.0/lib/opt/libmysql.lib")

void main()
{
 MYSQL *conn = mysql_init(NULL);
 if(conn == NULL)
  throw "error";
 if(mysql_real_connect(
  conn,
  "127.0.0.1",
  "user", "passwd",
  NULL,
  0,
  NULL,
  0) != conn)
  throw "error";

 int nRet = 0;

 char szInsert[] = "INSERT INTO db_transation_test.tbl_transactions SET amount=71, ses=72";
 nRet = mysql_real_query(conn, szInsert, sizeof(szInsert));
 if(nRet != 0)
 {
  printf("%s\n", mysql_error(conn));
  throw;
 }
 my_ulonglong inserted_id = mysql_insert_id(conn);

 ///////// the real issue of my post starts here /////////

 char szUpdateTemplate[] = "UPDATE db_transation_test.tbl_transactions SET amount=123 WHERE id_transaction=%lld";
 char szUpdate[1024];
 nRet = sprintf_s(szUpdate, sizeof(szUpdate), szUpdateTemplate, inserted_id);
 nRet = mysql_real_query(conn, szUpdate, nRet);
 if(nRet != 0)
 {
  printf("%s\n", mysql_error(conn));
  throw;
 }
 my_ulonglong updated_id = mysql_insert_id(conn); // returns ZERO instead of 'inserted_id' :(

 _getch();
}

How do I make this mysql_insert_id() function work in that case, or alternatively how do I UPDATE and SELECT in one statament?

Note that I’ve INSERTed a row in this example so it’ll make some row for you, so you can see..

Thank you!

EDIT:
Check out my latest reply to this question. I gave a much more clear description.

  • 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-13T20:59:23+00:00Added an answer on May 13, 2026 at 8:59 pm

    Your UPDATE statement does not cause the generation of an autoincremented id, so calling mysql_insert_id after you update makes no sense.

    As the docs say, mysql_insert_id() works on inserts statements that cause an auto incremented value to be generated, or an update/insert statement that calls `LAST_INSERT_ID(expr)^ directly.

    You’ve already fetched the inserted_id right after your insert statement, you don’t need to fetch it again.

    Edit, You are apparently asking about something else.

    You have something like
    UPDATE db_transation_test.tbl_transactions SET amount=123 WHERE username=someone;

    and you want to fetch the primary key of whatever row that that query updated ? Is that your actual question ?

    If so. No. Run a separate select query to fetch that ID.

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

Sidebar

Related Questions

From the screenshot featured on this page http://www.kde.gr.jp/~ichi/qt/designer-manual-3.html it seems like this functionality was
On this page in the Subversion manual , it says that When you commit
From this page: http://www.doctrine-project.org/documentation/manual/1_2/en/working-with-models#dealing-with-relations:creating-related-records You can see that it says $obj['property']; is the recommended
The manual page for XML::Parser::Style::Objects is horrible. A simple hello world style program would
How often should you use git-gc? The manual page simply says: Users are encouraged
I generally use a manual process: Look at the page, figure out the semantic
in CSS: The missing manual the author says that font-size: medium (or other size
Read some texts about locking in PHP. They all, mainly, direct to http://php.net/manual/en/function.flock.php .
In a world where manual memory allocation and pointers still rule (Borland Delphi) I
In the PHP manual, to show the syntax for functions with optional parameters, they

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.