|
-
Mar 23rd, 2009, 07:40 PM
#1
Thread Starter
Hyperactive Member
Last auto generate id
Is there a function to get the last generated MySQL record Id
Basically doing something like this
1. Enter book details
2. Details written to file with a MySQL auto generated key.
Now want to add
1. Borrowers of the book wtih the auto generated key identifiying which book a borrow has gpt their grubby hands on.
We are trying to determine via an online system which genre of books are the most popular with which sections of a school.
Hope that makes some sort of sense.
-
Mar 23rd, 2009, 09:44 PM
#2
Re: Last auto generate id
if you are inserting something and want the ID of the item you just inserted, you can use mysql_insert_id(), like so:
PHP Code:
mysql_query("INSERT INTO table (field1, field2) VALUES (value1, value2)"); $id = mysql_insert_id();
but, if you just want to get the very last ID in general (and haven't inserted anything), you can just do a regular query and sort it by the ID (descending), and limit it to 1 result.
Code:
SELECT id FROM table WHERE field=value ORDER BY id DESC LIMIT 1
hope that answers your question. the end of your post got me a little confused with what you're trying to do.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|