TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-01-2007, 12:52 AM   #1 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Application Error Last Inserted ID?

Hey,

I've been trying for a while to get the last inserted id of a table to use in my next query, the problem is mysql_insert_id seens to produce an empty return, any ideas?

Thanks
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 12-01-2007, 01:25 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Does the table you are inserting into contain a primary, auto increment index?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-01-2007, 01:47 AM   #3 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

Indeed it does although not in correlation to the other table.
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 12-01-2007, 02:47 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You'll have to paste me your INSERT statement so that I can see.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-01-2007, 02:53 AM   #5 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

Theres 4, but, i'm trying to do it with PHP if that's possible? It's just a standard insert query (forums based).
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 12-01-2007, 02:58 AM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Well, it's going to get the ID from the very last query, regardless of whether or not it's an INSERT or not. Do you happen to have another query in between the INSERT you want the ID from, and the mysql_insert_id call?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-01-2007, 01:31 PM   #7 (permalink)
The Wanderer
PHP Guru Advanced Programmer Zend Certified 
 
DragonBe's Avatar
 
Join Date: Nov 2007
Location: according to my wife: on the Net
Posts: 19
Thanks: 0
DragonBe is on a distinguished road
Default

Hey Winsrev,

A simple example might show you where you might have done things wrong.

PHP Code:
if ($conn mysql_connect('hostname:port''username''password')) {
  if (
$db mysql_select_db('database'$conn)) {
    
$query mysql_query(sprintf("INSERT INTO `table` SET `field_a`='%s', `field_b`='%s'"$clean_field_a$clean_field_b), $conn);
    
$last_insert_id mysql_insert_id($conn); // returns last auto_incremented id
    
$affected_rows mysql_affected_rows($conn); // returns how many rows were affected
  
} else {
    
$error_db mysql_errno($conn) . ": " mysql_error($conn);
  }
} else {
  
$error_db_conn mysql_errno($conn) . ": " mysql_error($conn);

Hope this cleared some of your questions.

Good luck,

DragonBe
Send a message via ICQ to DragonBe Send a message via Skype™ to DragonBe
DragonBe is offline  
Reply With Quote
Old 12-01-2007, 05:57 PM   #8 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

It's order is:
First Query
mysql_insert_id();
Second Query
Third Query
mysql_insert_id();
Fourth Query

Problem is the second mysql_insert_id produces nothing although the first one works.
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 12-01-2007, 06:06 PM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Is the third query not an INSERT? 'Cause if you want the ID from the second query then you should put it after the second query, not after the third.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-01-2007, 06:08 PM   #10 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

They're all inserts? There's no selects, updates, etc... Just inserts.
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 12-01-2007, 08:10 PM   #11 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

Actually, i just took a look at my query's and realised the problem is the first bit seen as php doesn't like giving line numbers of mysql errors. It's all fixed now, thanks for your help :D
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 12-01-2007, 09:02 PM   #12 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

Incidentally, you can often solve an odd problem like this by using straight SQL, e.g.

(using SQL to retrieve the ID value and store as a variable)
Code:
SELECT @last_id:= LAST_INSERT_ID();
Now whenever you need to retrieve that value or use it within an SQL statement (all within the same login session, e.g. same script execution of course), just refer to it using the MySQL variable @last_id. Several calls to mysql_query() later it will still be the same value.
SOCK is offline  
Reply With Quote
Old 12-01-2007, 09:22 PM   #13 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

Problem is, by the time that's called another record might have alread been inserted causing you to get an unexpected output.
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 12-01-2007, 09:58 PM   #14 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Isn't mysql_insert_id() a little dangerous if multiple people are using the system?
bdm is offline  
Reply With Quote
Old 12-01-2007, 10:07 PM   #15 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

Not really, it gets the id of exactly the last query, if you run it straight after the query that is. If you select last_inserted_id then chances are it'd be dangerous.
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 12-02-2007, 02:03 PM   #16 (permalink)
The Contributor
 
aristoworks's Avatar
 
Join Date: Nov 2007
Location: Nashville
Posts: 44
Thanks: 7
aristoworks is on a distinguished road
Asterix

Taken From: http://us3.php.net/mysql_insert_id

mysql_insert_id() converts the return type of the native MySQL C API function mysql_insert_id() to a type of long (named int in PHP). If your AUTO_INCREMENT column has a column type of BIGINT, the value returned by mysql_insert_id() will be incorrect. Instead, use the internal MySQL SQL function LAST_INSERT_ID() in an SQL query.
Send a message via AIM to aristoworks
aristoworks is offline  
Reply With Quote
The Following 3 Users Say Thank You to aristoworks For This Useful Post:
Haris (12-03-2007), Karl (12-02-2007), Wildhoney (12-03-2007)
Old 12-03-2007, 01:54 AM   #17 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by aristoworks View Post
Taken From: http://us3.php.net/mysql_insert_id

mysql_insert_id() converts the return type of the native MySQL C API function mysql_insert_id() to a type of long (named int in PHP). If your AUTO_INCREMENT column has a column type of BIGINT, the value returned by mysql_insert_id() will be incorrect. Instead, use the internal MySQL SQL function LAST_INSERT_ID() in an SQL query.
You deserve rep for it.

Now, I need to update my scripts.
Haris is offline  
Reply With Quote
Old 12-03-2007, 02:30 AM   #18 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Repped and thanked I must admit that I didn't even know that, and so it's a good piece of information to have!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-03-2007, 03:53 PM   #19 (permalink)
The Contributor
 
aristoworks's Avatar
 
Join Date: Nov 2007
Location: Nashville
Posts: 44
Thanks: 7
aristoworks is on a distinguished road
Default Thanks!

Thanks guys. Glad to help out. I too have some "updating" to do.

Jon
Send a message via AIM to aristoworks
aristoworks is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:07 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design