 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
12-01-2007, 12:52 AM
|
#1 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Posts: 133
Thanks: 6
|
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 
|
|
|
12-01-2007, 01:25 AM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
12-01-2007, 01:47 AM
|
#3 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Posts: 133
Thanks: 6
|
Indeed it does  although not in correlation to the other table.
|
|
|
12-01-2007, 02:47 AM
|
#4 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
12-01-2007, 02:53 AM
|
#5 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Posts: 133
Thanks: 6
|
Theres 4, but, i'm trying to do it with PHP if that's possible? It's just a standard insert query (forums based).
|
|
|
12-01-2007, 02:58 AM
|
#6 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
12-01-2007, 01:31 PM
|
#7 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Location: according to my wife: on the Net
Posts: 19
Thanks: 0
|
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
|
|
|
12-01-2007, 05:57 PM
|
#8 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Posts: 133
Thanks: 6
|
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.
|
|
|
12-01-2007, 06:06 PM
|
#9 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
12-01-2007, 06:08 PM
|
#10 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Posts: 133
Thanks: 6
|
They're all inserts? There's no selects, updates, etc... Just inserts.
|
|
|
12-01-2007, 08:10 PM
|
#11 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Posts: 133
Thanks: 6
|
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
|
|
|
12-01-2007, 09:02 PM
|
#12 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Posts: 154
Thanks: 31
|
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.
|
|
|
|
12-01-2007, 09:22 PM
|
#13 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Posts: 133
Thanks: 6
|
Problem is, by the time that's called another record might have alread been inserted causing you to get an unexpected output.
|
|
|
12-01-2007, 09:58 PM
|
#14 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Posts: 127
Thanks: 14
|
Isn't mysql_insert_id() a little dangerous if multiple people are using the system?
|
|
|
|
12-01-2007, 10:07 PM
|
#15 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Posts: 133
Thanks: 6
|
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.
|
|
|
12-02-2007, 02:03 PM
|
#16 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Location: Nashville
Posts: 44
Thanks: 7
|
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.
|
|
|
|
The Following 3 Users Say Thank You to aristoworks For This Useful Post:
|
|
12-03-2007, 01:54 AM
|
#17 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
Quote:
Originally Posted by aristoworks
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. 
|
|
|
|
12-03-2007, 02:30 AM
|
#18 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
12-03-2007, 03:53 PM
|
#19 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Location: Nashville
Posts: 44
Thanks: 7
|
Thanks!
Thanks guys. Glad to help out. I too have some "updating" to do.
Jon
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|