Example: I insert a row into the database with this using PHP built into PDO:
$sql = "INSERT INTO mytable (name, ok) VALUES ('john', '1')"; $this->dbh->exec($sql);
I need the id of this string. How can i get this?
If id is auto_increment , you can use PDO::lastInsertId :
id
auto_increment
PDO::lastInsertId
Returns the identifier of the last inserted row or the last value from the sequence of the object, depending on the driver.
So, in your case, something like this should do the trick:
$lastId = $this->dbh->lastInsertId();
Source: https://habr.com/ru/post/1415544/More articles:Two div side by side, right div fixed width - htmlNot applicable - jsf-2Architecture Selection ASP.Net Web API - architecturePDO - get the current inserted id - phpSamsung Galaxy notes that it does not accept xml file from large layout after updating to Android 4 - androidSolr increments result by field value - sortingIn JavaScript, why is the "reverse while" loop an order of magnitude faster than the "for"? - performancecannot follow the link for a boolean variable in android - javaHow to delete tables marked as "used" using phpmyadmin - sqlJava patterns do not understand - javaAll Articles