Jump to content
xisto Community
Sign in to follow this  
sagoral

Getting Last Id After I inserted a row

Recommended Posts

Hi,

I want to get last data id after i inserted a new row.. I can do it with this code:

mysql_query("INSERT INTO table VALUES(id, name)");$last_rows=mysql_fetch_array(mysql_query("SELECT * FROM table ORDER BY id DESC"));
But i want to learn is there any easy way to getting last id after i inserted a new row?

Share this post


Link to post
Share on other sites

Don't use an asterisk (*) when you only want one column (the ID). Secondly, use the LIMIT clause to retrieve only one entry, not every single entry within the table.

Share this post


Link to post
Share on other sites

Don't use an asterisk (*) when you only want one column (the ID). Secondly, use the LIMIT clause to retrieve only one entry, not every single entry within the table.


Thanks for your interesting but i know these... I chose the short way while writing my message :)

Share this post


Link to post
Share on other sites

Thanks for your interesting but i know these... I chose the short way while writing my message :)

 

You can get the last id by using the mysql function mysql_insert_id "http://dev.mysql.com/doc/refman/5.7/en/getting-unique-id.html;

 

Here is a sample code using the function.

 

<?php$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');if (!$link) {    die('Could not connect: ' . mysql_error());}mysql_select_db('mydb');mysql_query("INSERT INTO mytable (product) values ('kossu')");echo mysql_insert_id();?>

Share this post


Link to post
Share on other sites

You can get the last id by using the mysql function mysql_insert_id "http://dev.mysql.com/doc/refman/5.7/en/getting-unique-id.html;
Here is a sample code using the function.

<?php$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');if (!$link) {    die('Could not connect: ' . mysql_error());}mysql_select_db('mydb');mysql_query("INSERT INTO mytable (product) values ('kossu')");echo mysql_insert_id();?>

This is what i want exactly.. But i want to ask a question about using this function: Do we need to name column as "id"?
or never mind i will try it then i'll see :)

Thank you,

Share this post


Link to post
Share on other sites

Okay I understand, its getting the last id of auto_increment column.


You need to note that not all database servers support the call, this is only supported from MySQL environment. This gives me problem when I am working with a website that uses an abstract connector to the database. Since the function is database specific and no almost no databases support a method similar to what MySQL provides, half of the website collapses when assuming that the database server can return the last increment value for the auto increment columns.

Also, there is no need to worry that someone inserted a new record while you are fetching this value, this is connection instance related function and you will always get the last auto increment value that your query have generated.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.