PHP PDO Data_length始终返回"0". [英] PHP PDO Data_length always returns "0"

查看:92
本文介绍了PHP PDO Data_length始终返回"0".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我现在正在尝试以下两个小时左右,但无法正常工作.以下代码将始终返回"0".请,任何人都可以看到问题出在哪里.这应该是一种魅力.在PhpMyAdmin中,当我运行该语句时,它可以正常工作.

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.

实际上,这是此处有关SO的另一个问题的复制/粘贴代码,已被接受为有效答案.在此处中进行检查.

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

使用以下命令不会引发任何错误: error_reporting(-1);PDO::ERRMODE_EXCEPTION.

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

更新: 绝对,我从查询中仅获得第一行,其中Data_lenght为"0".我还测试了fetchAll,并且还在尝试访存时尝试访问Data_length索引.没有运气.

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

感谢您的帮助.

推荐答案

PDO始终返回"0",因为$row = $sth->fetch(PDO::FETCH_ASSOC);仅返回查询的第一行,而Data_length的索引为"0",因为它可以是任何其他值.

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.

这篇关于PHP PDO Data_length始终返回"0".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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