Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Good day.

I have a mysql query which returns the sum of values on a table.

PHP
357  $conn = mysqli_connect($servername, $username, $password, $dbname);
358  
359  SELECT FORMAT(SUM(Total_Amount),0) FROM(
360      (SELECT SUM(assign_inventory.`quantity` * assign_inventory.`price`) AS 'Total_Amount'
361      FROM assign_inventory WHERE assign_inventory.`project` = 'Apongbon Shopping Mall')
362      UNION ALL
363  	(SELECT SUM(contrator_payments.`amount`) AS 'Total_Amount'
364      FROM contrator_payments WHERE contrator_payments.`project_name` = 'Apongbon Shopping Mall')
365  	UNION ALL
366  	(SELECT SUM(petty_cash_expenses.`amount`) AS 'Total_Amount'
367      FROM petty_cash_expenses WHERE petty_cash_expenses.`project` = 'Apongbon Shopping Mall'))as Total_Amount;<pre>";?>
368  											<?php 	$result = mysqli_query($conn,$query); 
369  $data = mysqli_fetch_assoc($result);
370  echo $data;?


What I have tried:

I try to echo the result but I get the error "
( ! ) Notice: Array to string conversion in C:\wamp64\www\bosmak_properties\project_cost_details.php on line 370


Line 370 is the echo $data line.
Posted
Updated 25-Nov-20 1:02am
v3
Comments
Richard MacCutchan 25-Nov-20 5:22am    
You cannot use echo on anything other than simple strings. You need to echo each individual item in the returned data.

Returns from SQL queries, even if only a single value, are arrays.


The first element in your $data return, then, is $data[0], or, if you have the filed name, as in an associative array, $data['Total_Amount']

printr(), as in the previous answer, is useful in that you can see what the array is sending back and that is useful for at least two points:
1) it let's you see the extent of the data (or none!) without having to know details, even if it's a multi-dimensional array.
2) you have the correct indices for the values, not the least important of which is the name (if associative), the index, if not, or both if you grab it all. Including correct spelling and capitalization.

So the error's telling you precisely this: you want to echo an array (an object) and there's no rules for that.
 
Share this answer
 
Have you tried print_r($data);
 
Share this answer
 
Comments
akynyemi 25-Nov-20 3:51am    
Thanks @13178297. I have tried print_r($data); but it returns the required value but also shows =>FUNCTIONS(SUM(5,813,617) along with it which I do not want displayed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900