471,337 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

recall id after adding a query to MySQL database

Hello,

Lately I came accross this problem a couple of times, and I know there
must be a clever solution. However, I did not find it untill now.

When I add a entry to my mysql database from a PHP form, it gives the
document an 'id number', which is generated by MySQL. In my PHP script
I would like to continue with id, since that is easier. At the moment
I place a SELECT query in which I basically match the values I still
have in my PHP_POST with the values in the database (those I just
entered).

I have the feeling that there is a simple code for replacing those
lines of PHP script and unnecessary queries. Is there somebody who can
help me, please?

Jochem
Jul 17 '05 #1
2 3008
In article <2e**************************@posting.google.com >,
jd******@operamail.com (Jochem) wrote:
Hello,

Lately I came accross this problem a couple of times, and I know there
must be a clever solution. However, I did not find it untill now.

When I add a entry to my mysql database from a PHP form, it gives the
document an 'id number', which is generated by MySQL. In my PHP script
I would like to continue with id, since that is easier. At the moment
I place a SELECT query in which I basically match the values I still
have in my PHP_POST with the values in the database (those I just
entered).

I have the feeling that there is a simple code for replacing those
lines of PHP script and unnecessary queries. Is there somebody who can
help me, please?


Assuming the document ID number is the result of a MySQL auto_increment
field,

$result = mysql_query('INSERT INTO foo SET bar=1', $db);
$id = mysql_insert_id();
<http://php.net/mysql_insert_id>

or

$result = mysql_query('INSERT INTO foo SET bar=1', $db);
$result = mysql_query('SELECT LAST_INSERT_ID() AS id', $db);
$myrow = mysql_fetch_array($result);
$id = $myrow[id];

The mysql_insert_id() function will return the numeric value of the
auto_increment field of the last inserted record. The MySQL query for
LAST_INSERT_ID() will return the same.

Happy Holidays!

hth

--
Bulworth : fu***@fung.arg | My email address is ROT13 encoded, decode to mail
--------------------------|--------------------------------------------------
<http://www.phplabs.com/> | PHP scripts and thousands of webmaster resources!
Jul 17 '05 #2
Thanks that was my missing link!
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by DBNovice | last post: by
3 posts views Thread by Paradigm | last post: by
9 posts views Thread by noor.rahman | last post: by
5 posts views Thread by deaconj999 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.