I am writing a DB like this:
---------------------------------------------------------------------
| id (auto_incremented filed) | foo | bar |
---------------------------------------------------------------------
$sql_query = "INSERT INTO `table` (`foo`, `bar`) VALUES ('$string_1',
'$string_2')";
mysql_query($sql_query, $db_handle);
//how can I get id for the above inserted entry? is that possible I
get it with a single mysql instruction?
since each time I write a record into mysql, 'id' is incremented,
how can I get the id that an insertion corresponds to (can I get it
with a single mysql instruction)?
Thanks, 9 1916
On Fri, 20 Jul 2007 09:44:24 +0200, newbie <mi******@yahoo.comwrote:
I am writing a DB like this:
---------------------------------------------------------------------
| id (auto_incremented filed) | foo | bar |
---------------------------------------------------------------------
$sql_query = "INSERT INTO `table` (`foo`, `bar`) VALUES ('$string_1',
'$string_2')";
mysql_query($sql_query, $db_handle);
//how can I get id for the above inserted entry? is that possible I
get it with a single mysql instruction?
since each time I write a record into mysql, 'id' is incremented,
how can I get the id that an insertion corresponds to (can I get it
with a single mysql instruction)?
PHP:
mysql_insert_id();
MySQL:
mysql_query('SELECT LAST_INSERT_ID()');
--
Rik Wasmus
On Jul 20, 12:56 am, Rik <luiheidsgoe...@hotmail.comwrote:
On Fri, 20 Jul 2007 09:44:24 +0200, newbie <mitbb...@yahoo.comwrote:
I am writing a DB like this:
---------------------------------------------------------------------
| id (auto_incremented filed) | foo | bar |
---------------------------------------------------------------------
$sql_query = "INSERT INTO `table` (`foo`, `bar`) VALUES ('$string_1',
'$string_2')";
mysql_query($sql_query, $db_handle);
//how can I get id for the above inserted entry? is that possible I
get it with a single mysql instruction?
since each time I write a record into mysql, 'id' is incremented,
how can I get the id that an insertion corresponds to (can I get it
with a single mysql instruction)?
PHP:
mysql_insert_id();
Thanks for the answer. But I undersatnd mysql_insert_id() is to
'Retrieves the ID generated for an AUTO_INCREMENT column by the
previous INSERT query.'. How to deal with the situation that multi-
users insert rows concurrently?
Say, I have a user_insert.php page, and each user accessing this page
will insert a foo bar(say, both are user specified strings) a row, and
he/she get an ID back---and he/she need to know exactly which ID his/
her string corresponds to.
It seems that by using the following code can't guarantee atomicity.
mysql_query("INSERT INTO `table` (`foo`, `bar`) values ('example foo',
'example bar')");
mysql_insert_id();
Thanks
MySQL:
mysql_query('SELECT LAST_INSERT_ID()');
--
Rik Wasmus
newbie wrote:
Thanks for the answer. But I undersatnd mysql_insert_id() is to
'Retrieves the ID generated for an AUTO_INCREMENT column by the
previous INSERT query.'. How to deal with the situation that multi-
users insert rows concurrently?
That's only a problem if you use the SELECT method.
If you use mysql_insert_id or (whatever that command is) it gets the
last insert *on that connection*. So as long as two different users are
using two different connections, you don't have to worry about getting
back the wrong ID.
On Jul 20, 1:39 pm, Sanders Kaufman <bu...@kaufman.netwrote:
newbie wrote:
Thanks for the answer. But I undersatnd mysql_insert_id() is to
'Retrieves the ID generated for an AUTO_INCREMENT column by the
previous INSERT query.'. How to deal with the situation that multi-
users insert rows concurrently?
That's only a problem if you use the SELECT method.
If you use mysql_insert_id or (whatever that command is) it gets the
last insert *on that connection*. So as long as two different users are
using two different connections, you don't have to worry about getting
back the wrong ID.
ah, thanks!
..oO(Sanders Kaufman)
>newbie wrote:
>Thanks for the answer. But I undersatnd mysql_insert_id() is to 'Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.'. How to deal with the situation that multi- users insert rows concurrently?
That's only a problem if you use the SELECT method.
Nope.
>If you use mysql_insert_id or (whatever that command is) it gets the last insert *on that connection*.
So does LAST_INSERT_ID(). The last auto-generated ID is maintained by
the server on a per-connection basis. It doesn't matter if you use a
SELECT statement or an API call to retrieve it.
Micha
Michael Fesser wrote:
.oO(Sanders Kaufman)
>>If you use mysql_insert_id or (whatever that command is) it gets the last insert *on that connection*.
So does LAST_INSERT_ID(). The last auto-generated ID is maintained by
the server on a per-connection basis. It doesn't matter if you use a
SELECT statement or an API call to retrieve it.
Wow.
I coulda saved myself a lot of trouble a long time ago if I hadn't
learned that wrong... and taught it wrong.
since each time I write a record into mysql, 'id' is incremented,
how can I get the id that an insertion corresponds to (can I get it
with a single mysql instruction)?
PHP: mysql_insert_id();
Thanks for the answer. But I undersatnd mysql_insert_id() is to 'Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.'.
.... by the privious INSERT query *ON THIS CONNECTION*.
>How to deal with the situation that multi- users insert rows concurrently?
Don't deal with it: other users can (maliciously or not) insert
on other connections until they are blue in the face and not change
the result you get from mysql_insert_id().
>Say, I have a user_insert.php page, and each user accessing this page will insert a foo bar(say, both are user specified strings) a row, and he/she get an ID back---and he/she need to know exactly which ID his/ her string corresponds to.
If you *want* interference between users, you'll need to do something
like "SELECT max(id) FROM ... "
>It seems that by using the following code can't guarantee atomicity.
As long as no other queries are made between the two statements
listed *ON THIS CONNECTION*, you're OK.
>mysql_query("INSERT INTO `table` (`foo`, `bar`) values ('example foo', 'example bar')"); mysql_insert_id();
Gordon Burditt wrote:
>>Say, I have a user_insert.php page, and each user accessing this page will insert a foo bar(say, both are user specified strings) a row, and he/she get an ID back---and he/she need to know exactly which ID his/ her string corresponds to.
If you *want* interference between users, you'll need to do something
like "SELECT max(id) FROM ... "
Actually, I find myself having to build stuff in MySQL that *might* end
up on other DBMS's, so relying on auto-unique stuff in MySQL isn't feasible.
To keep my apps from breaking when they migrate like that, I've been
using a UID function. There are a lot of them out there if you query a
search on "UUID +PHP".
By generating a UUID to use as your primary key, instead of the
auto-increment thingy, YOU create the PK value as a "Universally Unique
ID". (Now - I haven't travelled the universe, but my *faith* tells me
it's a safe bet that I won't ever get a duplicate with one of these UUID
functions.)
This has the *added* benefit of avoiding having to do TWO conversations
with the DBMS - one to insert, and one to get the insert ID. Instead,
you just insert it with your own UUID as the PK. One shot, and the
programmer is in control, not the DBMS.
This becomes even *more* efficient if your DBMS and your HTTPd are not
on the same server.
function fnUUID(){
//Returns format: [12345678-1234-1234-123456789012]
$sRetVal = "";
$sRetVal .= sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ) );
return $sRetVal;
}
..oO(Sanders Kaufman)
>Actually, I find myself having to build stuff in MySQL that *might* end up on other DBMS's so relying on auto-unique stuff in MySQL isn't feasible.
Of course there are other - more portable - ways to create unique
numbers, i.e. sequences (some DBMS support them natively, in MySQL they
can be emulated).
>To keep my apps from breaking when they migrate like that, I've been using a UID function.
Not the best idea.
>By generating a UUID to use as your primary key, instead of the auto-increment thingy, YOU create the PK value as a "Universally Unique ID". (Now - I haven't travelled the universe, but my *faith* tells me it's a safe bet that I won't ever get a duplicate with one of these UUID functions.)
A UUID is not unique. It can be _considered_ unique, because a collision
is highly unlikely, but an AUTO_INCREMENT or a sequence _is_ unique.
>This has the *added* benefit of avoiding having to do TWO conversations
with the DBMS - one to insert, and one to get the insert ID.
True, but on the other hand you're wasting a lot of space. It's even
more waste if the UUID PK is referenced from other tables. There might
also be performance issues with the InnoDB storage engine (from what
I've read).
>Instead, you just insert it with your own UUID as the PK. One shot, and the programmer is in control, not the DBMS.
This becomes even *more* efficient if your DBMS and your HTTPd are not on the same server.
I don't consider that efficient. An AUTO_INCREMENT is efficient and
cheap. If it has to be portable, then using sequences can be the way.
Still more efficient than UUIDs.
Micha This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Oliv |
last post by:
Bonjour,
Est-il possible de se passer de l'instruction mysql_select_db() afin
de sélectionner une base de données par défaut dans MySQL ?
Je pense en particulier à l'association d'une base par...
|
by: Johnny |
last post by:
I was looking for information on how to import an excel file or other
text file into a MySql database. I have done this with both access
and SQL Server and am looking for a way to do this in...
|
by: Maxim. V. |
last post by:
Hello to everone,
As mentioned in this *guide(1), i've compiled mysql on my fedora Core
2 system, with the following spec's :
*(1) http://hulan.info/blog/index.php?itemid=576
...
|
by: Brett B |
last post by:
I just installed mysql on linux. If I open a terminal, su to root,
then type "mysql", I am able to connect to the server and run my
queries.
If I exit out of su so that I am my own id (baisley)...
|
by: Murphy |
last post by:
I am currently migrating a db from SQL Server & MySQL and ? (front end yet
to be decided upon) As I familiarise myself with MySQL populating tables
etc can be quite clumbersome. After reading the...
| |
by: Johannes Röckert |
last post by:
Hi,
can anyone help me on how to develop a program in c++ which does single step
execution using a callback between the asm commands? I need something like
debuggers do - a callback function...
|
by: Daz |
last post by:
Hi. I am trying to select data from two separate MySQL tables, where I
cannot use join, but when I put the two select queries into a single
query, I get an error telling me to check my syntax. Both...
|
by: bhing |
last post by:
please give me a step by step instructions on upgrading Mysql 3.23 to mysql 4.0
ive been wrestling with the results found on google but no one really give me a step by step instruction on how to...
|
by: John |
last post by:
Hi
What are the advantages actually achieved of managed code? I am not talking
of theory but in reality.
Thanks
Regards
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |