Supposing I am inserting a row into a table in MySql by php code, and my table has an ID which is not given but it is auto_increment – how can I retrieve the ID of the row I just inserted?
My query in php looks like this:
$query = "insert into " . $this->tabel . " values (false,";
And after this I am filling the query with the values given as parameters in my function.
However, I need to update an internal variable of my php class with the current ID. How can I obtain it?
Thank you
public function insert ($keyValueSet) {
$query = "insert into " . $this->tabel . " values (false,";
$connection = new mysqli(ADDRESS, USERNAME, PASSWORD, DB);
$cols = $this->getTableDesc();
// $query construction ......
$result = $connection->query($query);
echo "ID: " . mysql_insert_id ();
if ($result != 1)
echo "Exception";
$connection->close();
}
The incremented field is the first field (where i say: values (false,) and MySql does auto-increment this field, as I saw in the database.
For the default
mysql_connect()For procedural MySQLi e.g
mysqli_connect()For Object Oriented MySQLi e.g.
$db = new mysqli()