给出以下代码块:
$q = "
BEGIN;
DECLARE User_ID int
INSERT INTO user (field1,field2) values (value1,value2);
set User_ID = select LAST_INSERT_ID();
INSERT INTO table2 (field1,field2) values (value1,LAST_INSERT_ID());
select User_ID
COMMIT;";
$sth = PDO::prepare($q);
$sth->execute();
我该怎么做才能检索“User_ID”值? fetch和fetchAll只返回空数组,并且进程中没有抛出错误
编辑1 抱歉,应该更清楚......在返回结果之前有第二个INSERT语句。
答案 0 :(得分:1)
$ q =“ 开始; DECLARE User_ID int INSERT INTO user(field1,field2)值(value1,value2); set User_ID = select LAST_INSERT_ID(); INSERT INTO table2(field1,field2)值(value1,LAST_INSERT_ID() - 1); 选择User_ID COMMIT;“;
$ sth = PDO :: prepare($ q); $ sth->执行();