【问题标题】:phpmyadmin and mysql pdo lastInsertId()phpmyadmin 和 mysql pdo lastInsertId()
【发布时间】:2012-06-07 15:09:38
【问题描述】:

我有这个

$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。 任何帮助是极大的赞赏 谢谢

【问题讨论】:

  • 首先检查是否执行成功。因为如果不是,那么 0 可能就是正确的,对吧?
  • 是的,执行正常,一切正常,行被正确添加到数据库中

标签: php mysql pdo phpmyadmin lastinsertid


【解决方案1】:

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 仍然有效。

【讨论】:

    猜你喜欢
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2020-01-01
    • 2012-01-24
    • 2011-02-10
    • 2010-09-22
    • 2016-01-19
    相关资源
    最近更新 更多