[英]LAST_INSERT_ID Mysql is not working
我有以下查询
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 lid;
不返回。
它实际上不起作用,因为PDO不支持在一条语句中执行多个查询 。
您必须使用单独的查询再次执行,或者使用Frederic的注释中提到的lastInsertId()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.