简体   繁体   English

MYSQL更新记录错误:无法重新打开表(临时表)

[英]MYSQL Error updating record: Can't reopen table (Temporary table)

Before I get to my question I want you to know that I started learning MYSQL and PHP last week so it could be some of the worst coding you've seen in a while. 在提出问题之前,我想让您知道我上周开始学习MYSQL和PHP,因此这可能是您一段时间以来见到的最糟糕的编码。

Enough with the excuses, here's my problem, while Executing the following PHP script: 有了足够的借口,这是我的问题,同时执行以下PHP脚本:

           if($hdd!==0){
                  $sql="CREATE TEMPORARY TABLE custom AS SELECT * from `builds`;";
                $conn->query($sql);
                 if($hdd==1){
                 $sql="UPDATE custom SET HDD = null;";
                $conn->query($sql);
            }

                $conn->query("DROP PROCEDURE IF EXISTS ROWPERROW2;");
$conn->query("CREATE PROCEDURE ROWPERROW2()
BEGIN
DECLARE n INT DEFAULT 0;

DECLARE i INT DEFAULT 1;
DECLARE price INT DEFAULT 0;

SELECT COUNT(*) FROM custom INTO n;
SET i=1;

SET price=0;
WHILE i<=n DO 
SET price = (SELECT sum(`lowestPrice`) FROM ((SELECT `lowestPrice` FROM products WHERE name IN (SELECT `Motherboard` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `Cooling` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `PC_Case` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `PSU` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `GPU` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `Memory` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `SSD` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `HDD` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `CPU` from `custom` Where `id`=i ))) as A);
UPDATE custom SET `TotalPrice`=price WHERE `id`=i;
SET i = i + 1;
END WHILE;
End;");
                if ($conn->query("CALL ROWPERROW2();") === TRUE) {
    echo "Builds Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}
            }

I get the following error: 我收到以下错误:

Error updating record: Can't reopen table: 'custom' 更新记录错误:无法重新打开表:'custom'

"custom" is a temporary table, so I guess that's what causing the problem, how can I work my way around it? “ custom”是一个临时表,所以我想这就是导致问题的原因,我该如何解决呢? I can't just create a normal table each time because I want it to work on a multi-user environment 我不能每次都创建一个普通表,因为我希望它能在多用户环境中工作

Found a work around, it's not pretty, but it works. 找到了一个解决方法,它虽然不漂亮,但是可以。 What I did was creating multiple temporary tables based on the original temp table ('contents'), and then used them in the procedure so each table is used only once, like so: 我要做的是基于原始的临时表(“内容”)创建多个临时表,然后在过程中使用它们,因此每个表仅使用一次,如下所示:

                 $conn->query(" CREATE TEMPORARY TABLE t1 AS SELECT * FROM custom;");
                 $conn->query(" CREATE TEMPORARY TABLE t2 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t3 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t4 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t5 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t6 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t7 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t8 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t9 AS SELECT * FROM custom;");

And changed the procedure so it'd use each table only once: 并更改了程序,使其仅使用每个表一次:

$conn->query("CREATE PROCEDURE ROWPERROW2()
BEGIN
DECLARE n INT DEFAULT 0;

DECLARE i INT DEFAULT 1;
DECLARE price INT DEFAULT 0;

SELECT COUNT(*) FROM builds INTO n;
SET i=1;

SET price=0;
WHILE i<=n DO 
SET price = (SELECT sum(`lowestPrice`) FROM ((SELECT `lowestPrice` FROM products WHERE name IN (SELECT `Motherboard` from `t1` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `Cooling` from `t2` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `PC_Case` from `t3` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `PSU` from `t4` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `GPU` from `t5` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `Memory` from `t6` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `SSD` from `t7` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `HDD` from `t8` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `CPU` from `t9` Where `id`=i ))) as A);
UPDATE custom SET `TotalPrice`=price WHERE `id`=i;
SET i = i + 1;
END WHILE;
End;");
                if ($conn->query("CALL ROWPERROW2();") === TRUE) {
    echo "Builds Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM