Jump to content

[SOLVED] Getting last id of the auto-increment key


backyard

Recommended Posts

I have two tables and I need to get the last id of the primary key in one of the tables. I did some searching and found a solution that works.

 

$sql=mysql_query("INSERT INTO (table) (column) VALUES ('test')") or die (mysql_error());
$insert_id=mysql_insert_id();
$num = $insert_id + 1;   //need to increase the last id by one to match new entry in other table

 

This works well. The downside is in order for this to work an actual value is filed into a new row of (table) after the last id found in (column) thus giving me a blank row. While this isn't too bad, I could imagine I'll end up with a bunch of empty rows in my table. Is there a way to prevent this from happening -- i.e., a bunch of empty rows?

Link to comment
Share on other sites

You can also use the MySQL function LAST_INSERT_ID() if you run the second query after the one inserting into the auto incrementing table.

 

whats the difference between the tread starter mysql_insert_id() and yours

is there any?

Link to comment
Share on other sites

Thanks for the help. Maybe I should give more info on what I'm doing. I have some software an mp3 player that uses two tables where one stores the artist (table1) and the other stores the location of the music(table2) file. There's a column (matchart) in table2 that matches the artist with the music so matchart needs to lineup with the id of table1 thus I need to find the last_id of table1 and then increment this by one and insert this into matchart when the next person uploads music.

Link to comment
Share on other sites

??? ??? isn't the whole idea of relational databases to have relationships rather than near misses?  I fail to understand why you've chosen what seems to be such an abstruse approach.  Maybe someone else will understand ...

Link to comment
Share on other sites

basic setup for u.

users

------

user_id // int not null auto_increment pimary_key

user_name // varchar 50 not null

group_name // varchat 100 not null

date_added // int not null

logged_in // int not null

 

music

-----

music_id // int not null auto_increment pimary_key

user_id // int not nul

tune_name // varchar 50 not null

date_added // int not null

 

 

logged_on

---------

user_id // int not null auto_increment primark_kry

active // int not null

Link to comment
Share on other sites

??? ??? isn't the whole idea of relational databases to have relationships rather than near misses?  I fail to understand why you've chosen what seems to be such an abstruse approach.  Maybe someone else will understand ...

Are you referring to my post or someone elses? I'm new to php so I could very well be using a hammer to drive in a screw  ;) .

 

 

basic setup for u.

users

------

user_id // int not null auto_increment pimary_key

user_name // varchar 50 not null

group_name // varchat 100 not null

date_added // int not null

logged_in // int not null

 

music

-----

music_id // int not null auto_increment pimary_key

user_id // int not nul

tune_name // varchar 50 not null

date_added // int not null

 

 

logged_on

---------

user_id // int not null auto_increment primark_kry

active // int not null

 

So if I set all the tables columns to not null this'll save me from adding an extra row so in other words if just one column is not null I'll get the extra row? Some of the columns need to be null so could I just set a default value of something just to get around this?

Link to comment
Share on other sites

You can also use the MySQL function LAST_INSERT_ID() if you run the second query after the one inserting into the auto incrementing table.

whats the difference between the tread starter mysql_insert_id() and yours

is there any?

 

Probably not.  I didn't fully understand what the OP was trying to do, but it looked a little like he was using the mysql_insert_id() for another query.  But if one were to run another query, it would save a line or two of PHP code simply to use LAST_INSERT_ID() in the next SQL query.  However, looking at it in the light of day, it's doesn't appear relevant to the original problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.