douliexu5623 2016-10-08 13:15
浏览 45
已采纳

PHP PDO Data_length总是返回“0”

Well, I'm just trying the following for around two hours now and I can't get it to work. The following code will always return "0". Please, can anyone see where is the problem. This should work as a charm. And in PhpMyAdmin, when I run the statement it works correctly.

Actually, this is a copy/pasted code from another question here on SO which was accepted as a working answer. Check it out here.

EDIT: No errors are thrown using: error_reporting(-1); and PDO::ERRMODE_EXCEPTION.

UPDATE: Definitely, I'm getting only the first row from the query where the Data_lenght is "0". I've tested also fetchAll, and also trying to access the Data_length index while fetching. No luck.

<!DOCTYPE html>
<head>
<title></title>
</head>
<body>

<?php
try {
    error_reporting(-1);
    $host_name  = "my_hsot";
    $database   = "my_db";
    $user_name  = "my_user";
    $password   = "my_pwd";
    $conn = new PDO("mysql:host=$host_name;dbname=$database", $user_name, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $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 . "
" . $row["Data_length"];
    } catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
?>
</body>
</html>

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • duanlei5339 2016-10-08 20:07
    关注

    PDO always returns "0" because $row = $sth->fetch(PDO::FETCH_ASSOC); returns only the first row from the query, and the index of Data_length there is "0", as it could be any other value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘