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

时间:2022-03-18 01:41:12
    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;
    };

I have multiple rows and each time there is different value of $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;

In the end off all rows I would like to summarize all of them. Please advise and help ;)

最后,我想总结所有行。请指教和帮助;)

2 个解决方案

#1


1  

Your code should be as following:

您的代码应如下所示:

$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;

As the function is returning some value so you need to store that in a variable to use that.

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

#2


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;
 }

#1


1  

Your code should be as following:

您的代码应如下所示:

$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;

As the function is returning some value so you need to store that in a variable to use that.

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

#2


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;
 }