0

我有这个

$query = "INSERT INTO players VALUES (
                    UUID(),
                    :firstname,
                    :lastname,
                    :age,
                    :birthplace,
                    :height,
                    :weight,
                    :bats,
                    :throws,
                    :position,
                    :jersey,
                    :status,
                    :team,
                    :image_path
                )";
    $sth = $db->prepare($query);
    $sth->execute(array(
        ':firstname' => $firstname,
        ':lastname' => $lastname,
        ':age' => $age,
        ':birthplace' => $birthplace,
        ':height' => $height,
        ':weight' => $weight,
        ':bats' => $bats,
        ':throws' => $throws,
        ':position' => $position,
        ':jersey' => $jersey,
        ':status' => $status,
        ':team' => $team,
        ':image_path' => $image_path
    ));

    $id = $db->lastInsertId();
    return $id;

我正在尝试返回插入的最后一个 ID,而我得到的只是返回的 0。非常感谢任何帮助谢谢

4

1 回答 1

3

LAST_INSERT_ID()和朋友只适用于通过AUTO_INCREMENT列创建的整数 ID。您需要运行两个查询 - 首先

SELECT UUID() AS newuuid;

然后获取并存储此结果(例如 in $uuid),然后

"INSERT INTO players VALUES (
                    :uuid,
                    :firstname,
                    :lastname,
...
execute(array(
        ':uuid' => $uuid,
        ':firstname' => $firstname,
        ':lastname' => $lastname,
        ':age' => $age,

给你留下$uuid仍然有效的。

于 2012-06-02T21:45:49.250 回答