You need to use the LAST_INSERT_ID()
function: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
Eg:
INSERT INTO table_name (col1, col2,...) VALUES ('val1', 'val2'...); SELECT LAST_INSERT_ID();
This will get you back the PRIMARY KEY
value of the last row that you inserted:
The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client.
So the value returned by LAST_INSERT_ID()
is per user and is unaffected by other queries that might be running on the server from other users.
Related Posts:
- How to make MySQL table primary key auto increment with some prefix
- “INSERT IGNORE” vs “INSERT … ON DUPLICATE KEY UPDATE”
- LAST_INSERT_ID() MySQL
- MySQL date formats – difficulty Inserting a date
- How to do a batch insert in MySQL
- SQL – IF EXISTS UPDATE ELSE INSERT INTO
- How to set initial value and auto increment in MySQL?
- Column count doesn’t match value count at row 1
- ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)
- MySQL string replace
- MySQL Error “Operand should contain 1 column”
- How to use MySQL DECIMAL?
- Access denied for user ‘root@localhost’ (using password:NO)
- Which MySQL data type to use for storing boolean values
- ERROR 2003 (HY000): Can’t connect to MySQL server on localhost (10061)
- Cannot delete or update a parent row: a foreign key constraint fails
- MySQL server startup error ‘The server quit without updating PID file’
- PHP Connection failed: SQLSTATE[HY000] [2002] Connection refused
- Your password does not satisfy the current policy requirements
- MySQL: Access denied for user ‘test’@’localhost’ (using password: YES) except root user
- What is the definition of cardinality in SQL
- How to restart remote MySQL server running on Ubuntu linux?
- Host ‘xxx.xx.xxx.xxx’ is not allowed to connect to this MySQL server
- PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP
- Duplicating a MySQL table, indices, and data
- Selecting data from two different tables without using joins
- Difference between VARCHAR and TEXT in MySQL
- MySQL: Invalid use of group function
- SELECT * FROM multiple tables. MySQL
- How can I loop through all rows of a table? (MySQL)
- Simple Random Samples from a Sql database
- What does SQL clause “GROUP BY 1” mean?
- SQL select only rows with max value on a column
- Trying to perform MINUS operation in MySQL
- How do I find which transaction is causing a “Waiting for table metadata lock” state?
- mysql count duplicates
- phpmyadmin #1045 Cannot log in to the MySQL server. after installing mysql command line client
- MySQL config file location – redhat linux server
- How to change collation of database, table, column?
- Failed building wheel for mysql-python
- ERROR 1115 (42000): Unknown character set: ‘utf8mb4’
- Error Code: 1290. The MySQL server is running with the –secure-file-priv option so it cannot execute this statement
- Remove Primary Key in MySQL
- How to get the max of two values in MySQL?
- There can be only one auto column
- mysql: connection refused when trying to connect to localhost using remote IP
- Error: Duplicate entry ‘0’ for key ‘PRIMARY’
- Why are simple updates to wp_postmeta’s “_edit_lock” so slow?
- What’s a good strategy for planning WordPress DB growth?
- Complex meta query with 3 keys
- How to log mysql errors from wordpress core?
- wpdb Custom Meta Data with 2 conditions
- query to create woocommerce products from images
- How can I SQL update a meta_value to remove the dollar signs that were entered with the value? (so just the value remains)
- Configure WordPress to connect to Mysql DB using SSH tunneling
- How can I make wordpress suppress mysql errors?
- How to Create a Admin User for A WordPress Site via MySQL (PHPMyAdmin)?
- Rewrite database urls
- SQL error #1062 – Duplicate entry ‘1’ for key ‘PRIMARY’ [closed]
- Migrating from Existing MySQL Setup to WordPress
- How to split the a table in database?
- How do I join a $wpdb query with user meta using default value if not exist?
- mySQL statment count of post in each category and sub category
- Paypal Post IPN handeling nightmare
- Is there a page length limit?
- WordPress tables and aliases?
- What’s the sql for this condition?
- Create a form with custom autocomplete address field and CSV import values
- WordPress insert query is not working : Showing no Error
- How to set MYSQLI_OPT_INT_AND_FLOAT_NATIVE option
- Copy usermeta value where ID matches in the same table
- Some Post Content Does Not Show Up In DB Search
- How can I write a formal sql query to get data from a custom table
- How to insert dash (-) into database using wpdb and new_to_publish hook?
- MySQL: How to change url in posts from xxx.com to yyy.org?
- How can I view my site directly from WordPress dashboard?
- (WP List Table) How to sort by SKU and Custom Fields?
- Word Press and database from another server sync
- User set date and time from frontend doesn’t get stored into database
- get posts whether meta_key exists or not
- How to run SQL query from WordPress ‘WP Crontrol’ plugin
- How to bulk delete a certain part of all wordpress posts
- When I add metadata, metavalue length is added in to the value in some cases
- Changes not being reflected on new server
- WordPress Dutch characters encoding problem
- update_option returns false in AJAX, how to debug?
- WordPress database error Duplicate entry for key ‘slug’
- Select latest 2 articles, but only from given category
- MySQL – How to combine data from two tables into one?
- Stop my loop to register data twice
- `#1215 – Cannot add foreign key constraint
- How to restore: Database intact but folders deleted
- XML RPC post produces immediate revision with odd post_date_gmt
- Woocommerce: Grab total revenue of a product over all orders
- SQL command to export post_content from wp_posts using phpMyAdmin
- SQL Database Lost
- remove wordpress post links
- SQL get last entry of a specific gravity form
- mysqldump to a tar.gz
- Where is my mysql log on OS X?