PHP PDO检索MYSQL DB大小

时间:2014-01-31 18:43:11

标签: php pdo

尝试将我丑陋的mysql_转换为PDO但我在创建等效内容时遇到了困难。

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

我做了以下没有成功:

$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”]

这最终为我工作:

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

1 个答案:

答案 0 :(得分:0)

旧版本的PHP不允许解除引用返回的数组。请参阅this页面上的示例7。要解决此问题,请尝试以下操作:

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