大家好,我是 PHP 的新手。我有一个 php 代码,我想像这样在 mysql 中添加数据
表示例
id value1 value2 P_id
1 100 200 10
2 200 50 10
这是我的代码
$query = mysql_query("SELECT * from sample P_id = '10'");
while($row = mysql_num_rows($query)){
// Here is the condition I don't now what will be the code if I want to add
}
像这样,当我显示时,我希望值 1 的结果将是 300 和值 2 250 你能帮我吗,非常感谢
您的选择中缺少where
:
mysql_query("SELECT * from sample WHERE P_id = '10'");
这不是一个有效的 SQL 语句:
SELECT * from sample P_id = '10'
这是您需要遇到的正确查询。
mysql_query("SELECT SUM(value1) as Value1, SUM(value2) as Value2 FROM sample WHERE P_id = '10'");
while($row = mysql_num_rows($query)){
echo "Value 1:$row['Value1']; Value 2:$row['Value2']";
}
输出:
Value1 Value2
300 250