在提出问题之前,我想让您知道我上周开始学习MYSQL和PHP,所以这可能是您一段时间以来见过的最糟糕的编码。
有足够的借口,这是我在执行以下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;
}
}
我收到以下错误:
错误更新记录:无法重新打开表:'custom'
“ custom”是一个临时表,所以我想这就是导致问题的原因,我该如何解决呢?我不能每次都创建一个普通表,因为我希望它能在多用户环境下工作
答案 0 :(得分:0)
找到了解决方法,虽然不漂亮,但是可以。 我要做的是基于原始的临时表(“内容”)创建多个临时表,然后在过程中使用它们,因此每个表仅使用一次,就像这样:
$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;");
并更改了程序,使其仅使用每个表一次:
$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;
}