PHP PDO检索MYSQL DB大小 [英] PHP PDO retrieve MYSQL DB Size

查看:61
本文介绍了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";

这篇关于PHP PDO检索MYSQL DB大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆