8
$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 ???? ;

我怎么能做这个?

4

3 回答 3

21
$query = "INSERT INTO news VALUES (NULL, :param1 , :param2  )";
$stmt = $pdo->prepare($query);

$params = array(
    "param1" => $p['title'],
    "param2" => $p['body'],
);

$data = $stmt->execute($params);

所以你可以这样做以获得最后插入的 ID

$last_id = $pdo->lastInsertId();
于 2012-06-01T10:19:12.220 回答
4

利用 :

$last_insert_id = $pdo->lastInsertId();
于 2012-06-01T10:24:38.533 回答
2

你可以使用PDO::lastInsertId

$last_insert_id = $pdo->lastInsertId();
于 2012-06-01T10:14:27.183 回答