如何从php函数中获取价值(MySQL查询)

时间:2013-10-14 13:03:12

标签: php mysql function return

    function data1 ($d_year, $d_short, $d_name)
    {
$row=mysql_fetch_array(mysql_query("SELECT * FROM database WHERE country='$d_short' AND year='$d_year'")); 
echo"<b>$d_name ".$one=$row['sum']." EUR</b><div id='accitem'>".$row['paste']."</div>"; 
return $one;
    };

我有多行,每次都有不同的$ 1值。

data1 ("1", "uk", "United Kingdom"); $c_nat=$c_nat+$one;    
data1 ("1", "us", "United States"); $c_nat=$c_nat+$one;
echo $c_nat;

最后关闭所有行我想总结所有这些行。请指教和帮助;)

2 个答案:

答案 0 :(得分:1)

您的代码应如下所示:

$c_nat = 0; //this need to define first
$one = data1 ("1", "uk", "United Kingdom"); 
$c_nat=$c_nat+$one;    
$one = data1 ("1", "us", "United States"); 
$c_nat=$c_nat+$one;
echo $c_nat;

由于函数返回一些值,因此您需要将其存储在变量中以使用它。

答案 1 :(得分:0)

function data1 ($d_year, $d_short, $d_name)
 {
  $one = array();
 $result = mysql_query("SELECT * FROM database WHERE country='$d_short' AND year='$d_year'");       
while($row=mysql_fetch_array($result))
{ 
    $one[]=$row['sum'];
    echo"<b>$d_name ".$row['sum']." EUR</b><div id='accitem'>".$row['paste']."</div>"; 
}
return $one;
 }