Daniel Craick Daniel Craick -4 years ago 77
MySQL Question

I can't transform sum to integer in PHP

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

Answer Source

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) . '%';?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download