Php 将分组结果作为分离变量返回
我是mysql/php新手,正在尝试使用Php 将分组结果作为分离变量返回,php,mysql,Php,Mysql,我是mysql/php新手,正在尝试使用 $query = "SELECT expo_location, SUM(total_sale) FROM _sales_db WHERE day IN('Friday') GROUP BY expo_location"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){
$query = "SELECT expo_location, SUM(total_sale) FROM _sales_db WHERE day IN('Friday') GROUP BY expo_location";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "<span style='font-weight:normal;color:#cfcfcf;'>"."Total ". $row['expo_location']. " = $". number_format((float)$row['SUM(total_sale)'], 2, '.', '')."</span>";
echo "<br />";
}
我希望使用GROUPBY返回“expo_location”名称作为变量
Example
$Canton =
$Malone =
$Massena =
总而言之,将所有条目放在一列中,将它们分组并作为变量返回?下面的代码将为您创建一个关联数组,以便您可以执行类似$expo_locations['Canton']的操作,其中包含295.94
$query = "SELECT expo_location, SUM(total_sale) FROM _sales_db WHERE day IN('Friday') GROUP BY expo_location";
$result = mysql_query($query) or die(mysql_error());
$expo_locations = array();
// Print out result
while($row = mysql_fetch_array($result)){
$expo_locations[$row['expo_location']] = number_format((float)$row['SUM(total_sale)'], 2, '.', '');
}
您可以在执行记录循环时将提取的位置数据存储在数组中,然后简单地将所有收集的数据内爆为字符串
$query = "SELECT expo_location, SUM(total_sale) FROM _sales_db WHERE day IN('Friday') GROUP BY expo_location";
$result = mysql_query($query) or die(mysql_error());
$locations = array();
// Print out result
while($row = mysql_fetch_array($result)){
$locations[] = $row['expo_location'];
}
$locations_string = implode(', ', $locations); // String version
var_dump($locations); // Displays array content.
$query = "SELECT expo_location, SUM(total_sale) FROM _sales_db WHERE day IN('Friday') GROUP BY expo_location";
$result = mysql_query($query) or die(mysql_error());
$locations = array();
// Print out result
while($row = mysql_fetch_array($result)){
$locations[] = $row['expo_location'];
}
$locations_string = implode(', ', $locations); // String version
var_dump($locations); // Displays array content.