有问题。只是找不到问题。我有2个桌子,第一个是kampe:hold1,hold2,hold1goal,hold2goal和另一个hold:hold,fgoal,agol。我试图让所有hold1goal和hold2goal到fgoal和beforeal在hold表中。我是sql的新手。
<?php
include "config.php";
foreach($db->query('SELECT * FROM kampe') as $row){
$fgoal = $row['hold1goal'];
$agoal = $row['hold2goal'];
$hold = $row['hold1'];
$stmt = $db->prepare("UPDATE hold SET fgoal = :fgoal, agoal = :agoal WHERE hold = :hold");
$stmt->bindParam(':fgoal', $fgoal);
$stmt->bindParam(':agoal', $agoal);
$stmt->bindParam(':hold', $hold);
$stmt->execute();
/* print $agoal . "\n";
$fgoal = $row['hold2goal'];
$agoal = $row['hold1goal'];
$hold = $row['hold2'];
$stmt = $db->prepare("UPDATE hold SET fgoal = :fgoal, agoal = :agoal WHERE hold = :hold");
$stmt->bindParam(':fgoal', $fgoal);
$stmt->bindParam(':agoal', $agoal);
$stmt->bindParam(':hold', $hold);
$stmt->execute(); */
}
?>
这是结果。只有&#39; Odensevej&#39;变化。认为foreach循环会改变所有。
申佩
INSERT INTO `kampe` (`kampID`, `hold1`, `hold2`, `hold1goal`, `hold2goal`) VALUES
(1, 'marksvej', 'holdet', 0, 0),
(2, 'tervej', 'dfdf', 0, 0),
(3, 'Odensevej', 'holdet2', 2, 1),
(4, 'hest', 'hestener', 0, 0),
(5, 'holdet', 'tervej', 0, 0),
(6, 'dfdf', 'marksvej', 2, 6),
(7, 'Odensevej', 'hest', 0, 0),
(8, 'hestener', 'holdet2', 3, 1),
(9, 'dfdf', 'holdet', 0, 0),
(10, 'tervej', 'marksvej', 0, 0),
(11, 'hestener', 'Odensevej', 2, 1),
(12, 'holdet2', 'hest', 0, 0),
(13, 'Odensevej', 'holdet', 0, 0),
(14, 'dfdf', 'holdet2', 0, 0),
(15, 'hest', 'tervej', 2, 3),
(16, 'hestener', 'marksvej', 0, 0),
(17, 'holdet2', 'holdet', 0, 0),
(18, 'Odensevej', 'marksvej', 0, 0),
(19, 'hest', 'dfdf', 0, 0),
(20, 'tervej', 'hestener', 0, 0),
(21, 'hest', 'holdet', 0, 0),
(22, 'marksvej', 'holdet2', 0, 0),
(23, 'tervej', 'Odensevej', 0, 0),
(24, 'dfdf', 'hestener', 0, 0),
(25, 'holdet', 'hestener', 0, 0),
(26, 'hest', 'marksvej', 0, 0),
(27, 'tervej', 'holdet2', 0, 0),
(28, 'Odensevej', 'dfdf', 3, 2);
HOLD
INSERT INTO `hold` (`id`, `hold`, `leder`, `telefon`, `email`, `fgoal`, `agoal`, `p`) VALUES
(6, 'holdet', 'kim anov', '345345', 'kimanov@gmail.com', 0, 0, 0),
(20, 'marksvej', 'svend dellepude', '345345', 'kimanov@gmail.com', 0, 0, 1),
(61, 'tervej', 'Kim Test', '34343434', 'kim@anov.dk', 0, 0, 1),
(64, 'dfdf', 'dfdf', 'dfdf', 'kim@anov.dk', 0, 0, 0),
(67, 'Odensevej', 'Brian Anov', '34343434', 'brian@anov.dk', 3, 2, 2),
(69, 'holdet2', 'asda asdasd', '4234234', 'dsfsf@sdfsd.com', 0, 0, 0),
(70, 'hest', 'kim a', '323423', 'adas@asda.dk', 0, 0, 0),
(71, 'hestener', 'asdasd asdasd', '3423423423', 'safsdfasdasd.com', 0, 0, 2);
答案 0 :(得分:0)
这并没有直接回答您的问题,即您的查询结果循环失败的原因,但是......没有理由为什么要在PHP中循环来执行此操作。一个查询将执行SELECT和UPDATE:
update hold h
left join kampe k on
k.hold1 = h.hold
set
fgoal = k.hold1goal,
agoal = k.hold2goal
在混合中添加PHP循环会大大增加数据库中相对简单的UPDATE
查询的开销。