gpt4 book ai didi

php - LAST_INSERT_ID Mysql 不工作

转载 作者:行者123 更新时间:2023-11-29 17:30:44 24 4
gpt4 key购买 nike

我有以下查询

INSERT
INTO
profile(
fullname,
email,
cpfcnpj,
identity,
phone,
cellphoneone,
cellphonetwo,
birthday,
cep,
address,
)
VALUES(
: fullname, : email, : cpfcnpj, : identity, : phone, : cellphoneone, : cellphonetwo, : birthday, : cep, : address
);
SELECT
LAST_INSERT_ID() AS lid;

PHP 代码

$insertProfile = $database->driver()->prepare("
INSERT INTO wordprofile (fullname, email, cpfcnpj, identity, phone, cellphoneone, cellphonetwo, birthday, cep, address, city, state, reference, number, pay, contract) VALUES (:fullname, :email, :cpfcnpj, :identity, :phone, :cellphoneone, :cellphonetwo, :birthday, :cep, :address, :city, :state, :reference, :number, :pay, :contract); SELECT LAST_INSERT_ID() as lid;"
);

$insertProfile->bindValue(":fullname", $arg["name"]." ". $arg["lastname"]);
$insertProfile->bindValue(":email", $arg["email"]);
$insertProfile->bindValue(":cpfcnpj", $arg["cpfcnpj"]);
$insertProfile->bindValue(":identity", $arg["rg"]);
$insertProfile->bindValue(":phone", $tel);
$insertProfile->bindValue(":cellphoneone", $arg["cel1"]);
$insertProfile->bindValue(":cellphonetwo", $cel2);
$insertProfile->bindValue(":birthday", $birthday);
$insertProfile->bindValue(":cep", $arg["zip"]);
$insertProfile->bindValue(":address", $arg["address"]);

$insertProfile->execute();
$lastid = $insertProfile->fetch()[0];
echo $lastid; //nothing, if change to var_dump returns false

当我遇到 phpmyadmin 或 mysql 提示符时它可以正常工作。然而,同样的查询在 PHP 中返回 false;插入有效,只有 SELECT LAST_INSERT_ID() AS lit; 不返回。

最佳答案

它实际上不起作用,因为 PDO doesn't support several queries在一条语句中执行。
您必须使用单独的查询再次执行或使用方法 lastInsertId()正如 Frederic 的评论中提到的。

关于php - LAST_INSERT_ID Mysql 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50685287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com