Primary key return of an INSERT

4

I have tabela_x , where the first field ( campo1 ) is primary key and autoincrement .

I make a simple INSERT :

$ins = " INSERT INTO tabela_x ('campo2','campo3','campo4') VALUES ($c2, $c3, $c4) ";
$con -> query($ins)

I would like to get the return of this insert, with the value of campo1 .

I will always have to do a select right after insert just to get this value or is there any way I execute the query of the insert,     

asked by anonymous 09.04.2018 / 14:16

1 answer

5

Use the mysql_insert_id() function. Here's an example:

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());

Source: link

    
09.04.2018 / 14:19