-2

I have this code to extract sum of column records in mysql

<?php
$query = "select sum(sell) from store where sell!=0"; 
$result = $crud->getData($query);
$total_s = implode(" ",$result);
settype($total_s, "integer");?>
<p>Sum now is: <?php var_dump($result);//here my output in html?></p>
<?php //now i want to show in percent from max how much is sell
$maxm = 1000000;
$percent = $total_s / $maxm;
$percent_x = number_format( $percent * 100, 2 );
echo floor($percent_x) . '%';?>

Output in html:

array(1) { [0]=> array(1) { ["sum(sell)"]=> string(5) "25165" } } 

In error_log i have the message:

Array to string conversion in /home/dancr/public_html/app/out.php on line 36 
LF00
  • 27,015
  • 29
  • 156
  • 295

1 Answers1

1

You didn't need to trans number string to integer. the issue in your code is the $result_s.

<?php
$query = "select sum(sell) from store where sell!=0"; 
$result = $crud->getData($query);
$total_s = $result[0]["sum(sell)"];
$maxm = 1000000;
$percent = $total_s / $maxm;
$percent_x = number_format( $percent * 100, 2 );
echo floor($percent_x) . '%';?>
LF00
  • 27,015
  • 29
  • 156
  • 295