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()および友人は、列を介して作成された整数 ID に対してのみ機能しAUTO_INCREMENTます。最初に 2 つのクエリを実行する必要があります。

SELECT UUID() AS newuuid;

次に、この結果を取得して保存します (例: $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 に答える