Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, December 16, 2015

How to get the next auto-increment id in mysql

How to get the next auto-increment id in mysql


How to get the next id in mysql to insert it in the table

INSERT INTO payments (date, item, method, payment_code)  VALUES (NOW(), '1 Month', 'paypal', CONCAT("sahf4d2fdd45", id))  

Answer by Sarfraz for How to get the next auto-increment id in mysql


Use LAST_INSERT_ID() from your SQL query.

Or

You can also use mysql_insert_id() to get it using PHP.

Answer by Jacob for How to get the next auto-increment id in mysql


You can't use the ID while inserting, neither do you need it. MySQL does not even know the ID when you are inserting that record. You could just save "sahf4d2fdd45" in the payment_code table and use id and payment_code later on.

If you really need your payment_code to have the ID in it then UPDATE the row after the insert to add the ID.

Answer by Johan for How to get the next auto-increment id in mysql


You can get the next auto-increment value by doing:

SHOW TABLE STATUS FROM tablename LIKE Auto_increment  /*or*/  SELECT `auto_increment` FROM INFORMATION_SCHEMA.TABLES  WHERE table_name = 'tablename'  

Note that you should not use this to alter the table, use an auto_increment column to do that automatically instead.
The problem is that last_insert_id() is retrospective and can thus be guaranteed within the current connection.
This baby is prospective and is therefore not unique per connection and cannot be relied upon.
Only in a single connection database would it work, but single connection databases today have a habit of becoming multiple connection databases tomorrow.

Answer by Nick Pavluk for How to get the next auto-increment id in mysql


You can use mysql trigger

Answer by jondinham for How to get the next auto-increment id in mysql


You have to connect to MySQL and select a database before you can do this

$table_name = "myTable";   $query = mysql_query("SHOW TABLE STATUS WHERE name='$table_name'");   $row = mysql_fetch_array($query);   $next_inc_value = $row["AUTO_INCREMENT"];    

Answer by Angel for How to get the next auto-increment id in mysql


Solution:

CREATE TRIGGER `IdTrigger` BEFORE INSERT ON `payments`    FOR EACH ROW  BEGIN    SELECT  AUTO_INCREMENT Into @xId      FROM information_schema.tables      WHERE       Table_SCHEMA ="DataBaseName" AND      table_name = "payments";    SET NEW.`payment_code` = CONCAT("sahf4d2fdd45",@xId);    END;  

"DataBaseName" is the name of our Data Base

Answer by ssmusoke for How to get the next auto-increment id in mysql


What do you need the next incremental ID for?

MySQL only allows one auto-increment field per table and it must also be the primary key to guarantee uniqueness.

Note that when you get the next insert ID it may not be available when you use it since the value you have is only within the scope of that transaction. Therefore depending on the load on your database, that value may be already used by the time the next request comes in.

I would suggest that you review your design to ensure that you do not need to know which auto-increment value to assign next

Answer by Naresh Jain for How to get the next auto-increment id in mysql


In PHP you can try this:

$query = mysql_query("SELECT MAX(id) FROM `your_table_name`");  $results = mysql_fetch_array($query);  $cur_auto_id = $results['MAX(id)'] + 1;  

OR

$result = mysql_query("SHOW TABLE STATUS WHERE `Name` = 'your_table_name'");  $data = mysql_fetch_assoc($result);  $next_increment = $data['Auto_increment'];  

Answer by ravi404 for How to get the next auto-increment id in mysql


You can use

SELECT AUTO_INCREMENT  FROM information_schema.tables  WHERE table_name = 'table_name'  AND table_schema = DATABASE( ) ;  

or if you do not wish to use information_schema you can use this

SHOW TABLE STATUS LIKE 'table_name'  

To get the next insert id in php without using information_schema you can try the following(This is the better method, because its fast on large database)

$result = mysql_query("SHOW TABLE STATUS LIKE 'table_name'");  $row = mysql_fetch_array($result);  $nextId = $row['Auto_increment'];     

Update mysql_* functions have been deprecated

Answer by Hexchaimen for How to get the next auto-increment id in mysql


The top answer uses PHP MySQL_ for a solution, thought I would share an updated PHP MySQLi_ solution for achieving this. There is no error output in this exmaple!

$db = new mysqli('localhost', 'user', 'pass', 'database');  $sql = "SHOW TABLE STATUS LIKE 'table'";  $result=$db->query($sql);  $row = $result->fetch_assoc();    echo $row['Auto_increment'];  

Kicks out the next Auto increment coming up in a table.

Answer by Markus Malkusch for How to get the next auto-increment id in mysql


I suggest to rethink what you are doing. I never experienced one single use case where that special knowledge is required. The next id is a very special implementation detail and I wouldn't count on getting it is ACID safe.

Make one simple transaction which updates your inserted row with the last id:

BEGIN;    INSERT INTO payments (date, item, method)       VALUES (NOW(), '1 Month', 'paypal');    UPDATE payments SET payment_code = CONCAT("sahf4d2fdd45", LAST_INSERT_ID())       WHERE id = LAST_INSERT_ID();    COMMIT;  

Answer by user2940072 for How to get the next auto-increment id in mysql


use "mysql_insert_id()". mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value.

Below are the example of use:

  

I hope above example is useful.

Answer by user3814281 for How to get the next auto-increment id in mysql


mysql_insert_id();  

That's it :)

Answer by ??????_??_?_????? for How to get the next auto-increment id in mysql


SELECT id FROM `table` ORDER BY id DESC LIMIT 1  

Although I doubt in its productiveness but it's 100% reliable

Answer by Raa for How to get the next auto-increment id in mysql


Simple query would do SHOW TABLE STATUS LIKE 'table_name'


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.