我的表格是形容词和副词..它们都具有相同的结构,“单词”和“val”。和$ apart是一个用于保存爆炸函数结果的变量。
我有一些代码,这里..
$apart= explode(" ",$comment);
if(count($apart)> 0){
$amount= 0;
for ($i=0; $i < count($apart); $i++) {
$query = mysql_query("SELECT * FROM adjective WHERE word= '$apart[$i]' UNION SELECT * FROM adverb WHERE word= '$apart[$i]' ");
while($s=mysql_fetch_array($query)) {
$amount= $amount + $s['val'];
}
}
$total = $amount;
$q=mysql_query("INSERT INTO feedback (name, comment, val, date) VALUES ('$name', '$comment', '$total', '$date')");
if($q==1){
echo "<script>alert('Success!')</script>";
echo "<script>window.location='forum.php'</script>";
}
}
我的问题是...... 如何将两个爆炸的结果分成不同的变量? 因为我想改变公式如: $ amount = $ amount + $形容词['val'] * $ adverb ['val'];
对不起,如果我的解释很糟糕...... 我多次更改了代码但是以错误结束了.. 有人可以帮我吗?
答案 0 :(得分:0)
替换查询
SELECT * FROM adjective WHERE word = '$apart[$i]'
UNION
SELECT * FROM adverb WHERE word = '$apart[$i]'
带
SELECT adv.val * adj.val as factor
FROM adverb adv, adjective adj
WHERE adv.word = adj.word AND adj.word = '$apart[$i]';
如果你想获得所有行的总和,你可以使用SUM函数,但是在表APART中包含所有单词并且你可以使用它
会很好SELECT SUM(adv.val * adj.val)
FROM adverb adv, adjective adj, apart ap
WHERE adv.word = adj.word AND adj.word = ap.word;