按键访问数组的值

时间:2014-01-25 05:34:06

标签: php arrays

我有这个代码显示截至当前日期的总销售额,但它以这种方式显示

Array ( 
       [Date] => 2014-01-25 
       [TotalSales] => 7
      )

有什么办法,我可以用这种方式展示总销售:7和日期:2014-01-25?

<?php


   $host = 'localhost';
   $user = 'root';
   $passwd = '';
   $database = 'p_database';
   $connect = mysql_connect($host,$user,$passwd) or die("could not connect to database");

    $query = "SELECT DATE(order_time) AS Date, SUM(Quantity) AS TotalSales
    FROM ss_orders,ss_ordered_carts
    WHERE DATE(order_time) = DATE(NOW())
    group by date;";

    mysql_select_db($database,$connect);
    $result = mysql_fetch_assoc(mysql_query($query));
    print_r($result);
?>

3 个答案:

答案 0 :(得分:3)

您正在使用print_r打印有关变量的人类可读信息。您可以按名称访问值

echo "TotalSale: ".$result['TotalSales'];
echo "Date: ".$result['Date'];

答案 1 :(得分:2)

试试这段代码。

echo "Total Sale:{$result['TotalSales']}<br>";
echo "Date:{$result['Date']}<br>";

答案 2 :(得分:2)

试试这个

$result = mysql_fetch_assoc(mysql_query($query));
echo "Total Sale:".$result['TotalSales']." And Date:".$result['Date'];

这将根据您的需要输出Total Sale:7 And Date:2014-01-25