PHP PDO检索MYSQL DB大小

问题描述:

尝试将丑陋的mysql_转换为PDO,但是创建等效项时遇到困难.

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";

这也不会说明$ Row ["Index_length"]

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

这最终为我工作:

$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";

较早版本的PHP不允许取消引用返回的数组.请参见页面上的示例7.要解决此问题,请尝试以下操作:

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";