user3234335
user3234335

Reputation: 47

Access array's values by its key

I have this code which shows the total sale up till the current date , but it shows in 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);
?>

Upvotes: 4

Views: 58

Answers (3)

Satish Sharma
Satish Sharma

Reputation: 9635

try this

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

this will output as you desire Total Sale:7 And Date:2014-01-25

Upvotes: 2

Dulitha K
Dulitha K

Reputation: 2088

Try this code.

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

Upvotes: 2

Linga
Linga

Reputation: 10555

You are using print_r which Prints human-readable information about a variable . You can access the value by name

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

Upvotes: 3

Related Questions