如何使用php从数据库中添加结果

时间:2013-10-29 13:03:25

标签: php mysql operators

大家好我是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
}

像这样我希望value1的结果将是300和value2 250当我显示你能帮我这个非常感谢

3 个答案:

答案 0 :(得分:0)

您在选择中错过了where

mysql_query("SELECT * from sample WHERE P_id = '10'");

答案 1 :(得分:0)

这是您需要运行的正确查询..

   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

答案 2 :(得分:0)

这不是有效的SQL语句:

SELECT * from sample P_id = '10'