簡體   English   中英

phpmyadmin和mysql pdo lastInsertId()

[英]phpmyadmin and mysql pdo lastInsertId()

我有這個

$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。 任何幫助都非常感謝

LAST_INSERT_ID()和朋友僅適用於通過AUTO_INCREMENT列創建的整數ID。 您需要運行兩個查詢-首先

SELECT UUID() AS newuuid;

然后獲取並存儲此結果(例如,在$uuid ),然后

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

剩下的$uuid仍然有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM