Jump to content

Database size with return output


mark107

Recommended Posts

Hi all.

I need some help with my PHP code. I have got a problem with the code, because it did not come with MB, KB or GB as the return output. I'm only getting which it is "60.1" as the return output.

Here is the code:

$db_name = "dbuser_table";
$mailbox = $link->prepare("SELECT table_schema,ROUND(SUM(data_length + index_length) *1024, 2) AS dbsize FROM information_schema.TABLES WHERE table_schema = '$db_name'");
$mailbox->execute();
$data = $mailbox->fetch(PDO::FETCH_ASSOC);
$db_size = $data['dbsize'];
echo $db_size;

 

Here is what I want to achieve as the return output:

60.1MB

 

The only way I can do is convert it to byte, but I cant convert to byte because I'm keep getting "60.1" as the return output.

Do you know how I can get the return output of the database size with kb, mb or gb?

What do I need to use to get the return output of the DB size with the kb, mb or gb?

Thanks in advance.

Edited by mark107
Link to comment
Share on other sites

  • mark107 changed the title to Database size with return output
1 hour ago, mark107 said:

The only way I can do is convert it to byte,

data_length and index_length are already in bytes, according to the documentation, which makes your query not really make any sense (why multiple a byte value by 1024?).

Have your query just select the byte values, then format them into kb/mb/gb with PHP.  You can find plenty of functions that will format a size value online.

$mailbox = $link->query("SELECT data_length + index_length AS dbsize FROM information_schema.TABLES WHERE table_schema = '$db_name'");
$db_size = $mailbox->fetchColumn();
$db_size = formatBytes($db_size);

 

Link to comment
Share on other sites

4 minutes ago, kicken said:

data_length and index_length are already in bytes, according to the documentation, which makes your query not really make any sense (why multiple a byte value by 1024?).

Have your query just select the byte values, then format them into kb/mb/gb with PHP.  You can find plenty of functions that will format a size value online.

$mailbox = $link->query("SELECT data_length + index_length AS dbsize FROM information_schema.TABLES WHERE table_schema = '$db_name'");
$db_size = $mailbox->fetchColumn();
$db_size = formatBytes($db_size);

 

Oh right, well I just copied the code from other site so I must have got it wrong. I want to query the select of the byte values so I can convert it to MB easily. 

Could you please show me how I can use the code to select the bytes in example code?

I did look on the link you sent you, but I need to query the byte values first then format them into kb, mb or gb using with PHP.

Link to comment
Share on other sites

On 3/16/2023 at 11:38 AM, requinix said:

kicken already did that in his code. What problems are you having with trying to use it?

The problem I am having is I am getting the size of the database but without the KB, MB and GB which I need it.

How I can use the code to select the bytes in the database?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.