prepare($query); $params = ar...">

PDO - get the current inserted identifier

$query = "INSERT INTO news VALUES (NULL, :param1 , :param2 )"; $stmt = $pdo->prepare($query); $params = array( "param1" => $p['title'], "param2" => $p['body'], ); $data = $stmt->execute($params); // here i would like get current inserted ID. Is possible? $id = $data->id ???? ; 

How can i do this?

+6
source share
3 answers
 $query = "INSERT INTO news VALUES (NULL, :param1 , :param2 )"; $stmt = $pdo->prepare($query); $params = array( "param1" => $p['title'], "param2" => $p['body'], ); $data = $stmt->execute($params); 

so you can do this to get the last inserted id

 $last_id = $pdo->lastInsertId(); 
+17
source

Using:

 $last_insert_id = $pdo->lastInsertId(); 
+2
source

You can use PDO :: lastInsertId

 $last_insert_id = $pdo->lastInsertId(); 
+1
source

Source: https://habr.com/ru/post/1415542/


All Articles