项目:
跨多个相关表重复行数据。
问题:
在 php 中,使用第一个mysqli_multi_query后,我似乎无法从 mysqli_insert_id 获得 id结果。
地位:
我已经使用 phpmyadmin 成功查询了以下内容(手动将 unit_id 替换为 1,将 $unit_id1 替换为 61(下一个对应)):
PHPMYADMIN
CREATE TEMPORARY TABLE tmp
SELECT `unit_id`,
`title`,
`status_id`,
`category_id`,
`tags`,
`access_id`
FROM unit_genData
WHERE `unit_id` = 1;# 1 row affected.
ALTER TABLE tmp
DROP COLUMN `unit_id`;# 1 row affected.
UPDATE tmp
SET `title` = 'DUPLICATE';# 1 row affected.
INSERT INTO unit_genData
SELECT 0,tmp.*
FROM tmp;# 1 row affected.
DROP TABLE tmp;# MySQL returned an empty result set (i.e. zero rows).
CREATE TEMPORARY TABLE tmp
SELECT `ad_id`,
`unit_id`,
`ad_title`,
`ad_image`,
`ad_img_caption`,
`ad_brief_desc`,
`ad_btn_text`
FROM unit_promoContent
WHERE `unit_id`=1;# 1 row affected.
ALTER TABLE tmp
DROP COLUMN `ad_id`;# 1 row affected.
UPDATE tmp
SET `unit_id` = 61;# 1 row affected.
INSERT INTO unit_promoContent
SELECT 0,tmp.*
FROM tmp;# 1 row affected.
DROP TABLE tmp;# MySQL returned an empty result set (i.e. zero rows).
PHP
注意:第一个 multi_query 成功复制了第一个表...第二个 multi_query 取决于 mysqli_insert_id 结果。
$sql1 = "CREATE TEMPORARY TABLE tmp
SELECT `unit_id`,
`title`,
`status_id`,
`category_id`,
`tags`,
`access_id`
FROM ".ID_TABLE."
WHERE `unit_id` = " . $id . ";
ALTER TABLE tmp
DROP COLUMN `unit_id`;
UPDATE tmp
SET `title` = 'DUPLICATE';
INSERT INTO ".ID_TABLE."
SELECT 0,tmp.*
FROM tmp;
DROP TABLE tmp;
";
$result = mysqli_multi_query($dbc,$sql1)
or die(mysqli_error($sql1));
$unit_id1 = mysqli_insert_id($dbc); // Store new unit_id as var // Tab 2 :: Promo Content
$sql2 = "CREATE TEMPORARY TABLE tmp
SELECT `ad_id`,
`unit_id`,
`ad_title`,
`ad_image`,
`ad_img_caption`,
`ad_brief_desc`,
`ad_btn_text`
FROM unit_promoContent
WHERE `unit_id`=" . $id . ";
ALTER TABLE tmp
DROP COLUMN `ad_id`;
UPDATE tmp
SET `unit_id` = ". $unit_id1 .";
INSERT INTO unit_promoContent
SELECT 0,tmp.*
FROM tmp;
DROP TABLE tmp;
";
$result = mysqli_multi_query($dbc,$sql2)
or die(mysqli_error($sql2));