I have the follow query
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 Code
$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
It works properly when i run into phpmyadmin or mysql prompt. However that same query returns false in PHP; that insert works, only SELECT LAST_INSERT_ID() AS lid;
does not returns.
答案 0 :(得分:0)
It doesn't work actually because PDO doesn't support several queries executed in one statement.
You have to make another execute with a separated query or use the method lastInsertId()
as mentioned in the comments by Frederic.