tags:

views:

78

answers:

1

Hi. Couldn't find a method like the int executeUpdate(String sql, int autoGeneratedKeys) in php.

I want to insert data to the database, and then get an autogenerated int key in return (the ID).

Thanks :)

Oh, and PS: it has to be a method from the mysqli class =)

+4  A: 

See mysql_insert_id:

mysql_insert_id — Get the ID generated from the previous INSERT operation

Example (from the manual):

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

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

To retrieve the last auto-generated key using mysqli, see mysqli->insert_id

karim79
thanks. but I forgot to say that I can only use the mysqli class
Johannes
http://sk.php.net/manual/en/mysqli.insert-id.php
Lukáš Lalinský
I can see now that this method is also available in the mysqli.. thanks a lot ;)
Johannes