Get ID of a line that was just created in PHP

3

Hey guys, So I have a problem that is as follows, I have a table players , a table training and a helper table treadmill (where will I see if some player is currently training) The question is, how can I do that by the time I create a workout, this auxiliary table is populated with the workout ID I just created and the id of the players. For example, in my interface I select a training and select players that will participate, clicking on the button "Start workouts" I want that, besides creating a column in the table training , with auto-increment ID , I want you to create a line with the ID that has just been generated in the player_treater table. I hope I have illustrated well haha. In java it would be using Statement.RETURN_GENERATED_KEYS, but in PHP I no longer handle it. Does anyone know how to do it?

    
asked by anonymous 10.11.2015 / 19:44

2 answers

8

If you are using the MySQLi extension:

mysqli_query( $con,"INSERT INTO jogadores(nome, idade) VALUES ('Pedro',35)" );
$id = mysqli_insert_id( $con ); // Aqui obtemos o ID do registro inserido

For PDO:

$id = $con->lastInsertId();

If you are using the MySQL extension, is not recommended :

>
$id = mysql_insert_id();
    
10.11.2015 / 19:57
1

You can use the following SQL:

SELECT LAST_INSERT_ID() as id FROM tabela
    
10.11.2015 / 19:51