0

Trying to convert my ugly mysql_ into PDO but I'm having difficulty creating the equivalent.

$rows = mysql_query("SHOW TABLE STATUS"); 
$dbSize = 0; 
while ($row = mysql_fetch_array($rows)) { 
$dbSize += $row['Data_length'] + $row['Index_length']; 
} 
$decimals = 2;  
$mbytes = round($dbSize/(1024*1024),$decimals);
echo "$dbSize";

I've done the following without success:

$sth = $conn->query('SHOW TABLE STATUS');
$dbSize = 0; 
$dbSize = $sth->fetch(PDO::FETCH_ASSOC)["Data_length"];
$decimals = 2;  
$mbytes = round($dbSize/(1024*1024),$decimals);
echo "$dbSize";

This also wouldn't account for the $Row["Index_length"]

This ended up working for me:

$sth = $conn->query("SHOW TABLE STATUS");
$dbSize = 0;
$Result = $sth->fetchAll();
 foreach ($Result as $Row){
  $dbSize += $Row["Data_length"] + $Row["Index_length"];  
  }
$decimals = 2;  
$mbytes = round($dbSize/(1024*1024),$decimals);
echo "$dbSize";
Cœur
  • 37,241
  • 25
  • 195
  • 267
Carlo
  • 188
  • 2
  • 12
  • what does your `var_dump($dbSize)` say? the array dereferencing directly on method return is only allowed in php >= 5.4 – dhavald Jan 31 '14 at 18:47
  • With the mysql_ code I get int(5990478), the PDO code returns blank.php -version PHP 5.3.3 – Carlo Jan 31 '14 at 18:58
  • and dump of just `$sth->fetch(PDO::FETCH_ASSOC)`? – dhavald Jan 31 '14 at 18:59
  • array(18) { ["Name"]=> string(25) "tblAVSAP_AVSAPAssessments" ["Engine"]=> string(6) "MyISAM" ["Version"]=> string(2) "10" ["Row_format"]=> string(7) "Dynamic" ["Rows"]=> string(1) "0" ["Avg_row_length"]=> string(1) "0" ["Data_length"]=> string(1) "0" ["Max_data_length"]=> string(15) "281474976710655" ["Index_length"]=> string(4) "1024" ["Data_free"]=> string(1) "0" ["Auto_increment"]=> string(1) "1" ["Create_time"]=> string(19) "2013-12-18 14:43:31" ["Update_time"]=> string(19) "2013-12-18 14:43:31" ["Check_time"]=> NULL ["Collation"]=> string(15) "utf8_unicode_ci" ["Checksum"]=> (...) – Carlo Jan 31 '14 at 19:14

1 Answers1

0

Older versions of PHP does not allow dereferencing returned arrays. See example 7 on this page. To fix, try this:

$sth = $conn->query('SHOW TABLE STATUS');
$dbSize = 0;
$row = $sth->fetch(PDO::FETCH_ASSOC);
$dbSize = $row["Data_length"];
$decimals = 2;  
$mbytes = round($dbSize/(1024*1024),$decimals);
echo "$dbSize";
dhavald
  • 524
  • 4
  • 12
  • Thanks for the heads-up, I made some minor edits to your suggestion as seen in my starter post. – Carlo Jan 31 '14 at 21:00