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 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同