mark107 Posted March 15, 2023 Share Posted March 15, 2023 (edited) 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 March 15, 2023 by mark107 Quote Link to comment https://forums.phpfreaks.com/topic/316011-database-size-with-return-output/ Share on other sites More sharing options...
kicken Posted March 16, 2023 Share Posted March 16, 2023 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); Quote Link to comment https://forums.phpfreaks.com/topic/316011-database-size-with-return-output/#findComment-1606546 Share on other sites More sharing options...
mark107 Posted March 16, 2023 Author Share Posted March 16, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/316011-database-size-with-return-output/#findComment-1606547 Share on other sites More sharing options...
requinix Posted March 16, 2023 Share Posted March 16, 2023 10 hours ago, mark107 said: Could you please show me how I can use the code to select the bytes in example code? kicken already did that in his code. What problems are you having with trying to use it? Quote Link to comment https://forums.phpfreaks.com/topic/316011-database-size-with-return-output/#findComment-1606551 Share on other sites More sharing options...
mark107 Posted March 20, 2023 Author Share Posted March 20, 2023 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? Quote Link to comment https://forums.phpfreaks.com/topic/316011-database-size-with-return-output/#findComment-1606666 Share on other sites More sharing options...
requinix Posted March 21, 2023 Share Posted March 21, 2023 You've restated the problem like you first did last week. What progress have you made with the example code that has been given to you? What code do you have now? What is happening with it, and what were you expecting to happen? Quote Link to comment https://forums.phpfreaks.com/topic/316011-database-size-with-return-output/#findComment-1606680 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.