Search code examples
phpmysqlmegabyte

Get total size in MB from database


I'm trying to get and show total size of row from database,

PHP:

$swall = $db->super_query("SELECT sum(size) as sum FROM dle_photo_post");
$tpl->set("{swall}", $swall['sum']);

With above code, my result is like his: 82447456

Example 2: With this Code:

$swall = $db->super_query("SELECT sum(size)/1024 as sum FROM dle_photo_post");
$tpl->set("{swall}", $swall['sum']);

I got this: 80515.0938

but i need to show total in MB, like this: 80 MB

how i can show total size result in megabyte?


Solution

  • $swall = $db->super_query("SELECT sum(size)/1024/1024 as sum FROM dle_photo_post");
    $tpl->set("{swall}", number_format($swall['sum'], 0));
    

    Then you can use number_format (http://www.php.net/manual/en/function.number-format.php) to format the size.