PDO - 从事务中检索变量值?

时间:2011-09-15 19:27:29

标签: php mysql database transactions pdo

给出以下代码块:

$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语句。

1 个答案:

答案 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->执行();