Access to access array values ​​by its key

I have this code that shows the total sale until the current date, but shows it this way

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

Is there any way where I can show in this way Total Sale: 7 And Date: 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);
?>
+4
source share
3 answers

You use print_rone that prints human-readable variable information. You can access the value by name

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

Try this code.

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

try it

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

it will be displayed as you wish Total Sale:7 And Date:2014-01-25

+2
source

Source: https://habr.com/ru/post/1523659/


All Articles