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 只是返回空数组,并且过程中没有抛出任何错误
EDIT 1 抱歉,应该更清楚...在返回结果之前还有第二个 INSERT 语句。
Given the following block of code:
$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();
How the heck to do I retreive the value "User_ID" ? fetch and fetchAll just return empty arrays, and there are no errors thrown in the process
EDIT 1 Sorry, should have been more clear... there is a second INSERT statement before returning the results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$q = "
开始;
声明 User_ID int
INSERT INTO 用户(字段1,字段2)值(值1,值2);
设置 User_ID = 选择 LAST_INSERT_ID();
INSERT INTO table2 (field1,field2) 值 (value1,LAST_INSERT_ID() - 1);
选择用户 ID
提交;";
$sth = PDO::prepare($q);
$sth->execute();
$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() - 1);
select User_ID
COMMIT;";
$sth = PDO::prepare($q);
$sth->execute();