本文介绍了PDO提取仅返回第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新2:我恳请取消将此问题标记为重复,因为它不是另一个问题的重复,在进行一些研究后,我发现了该问题并提供了有效的答案,将我的问题标记为重复的人没有提供说明代码为他工作.好吧,它对他有用,但对我却不起作用.我还读了许多问题,当有人测试该代码对他有用时,他只是在注释中写上"对我有用"之类的注释,或类似内容.而不是标记我的问题,以后再发表以下评论:

UPDATE 2:I kindly asked to unmark this question as duplicate as it is not a duplicate of the other question, which after some research I discovered the problem and provided an effective answer, which the person that marked my question as duplicate didn't provide stating that the code worked for him. Well, it is working for him, but it is not working for me. I also read many many questions where when someone tests the code and it works for him, he just puts a note in the comments like this "It works for me", or something similar. And instead of marking my question and later posting the following comment:

"您应该做的是编辑另一个问题,而不是使用相同的代码重新发布.抱歉,我不会重新打开该问题.昨天我在您的另一个问题下发表了一些评论,但没有打扰,所以我最终删除了它们.声明自从我测试以来,您的代码没有任何问题."

..也许他应该做的只是在评论中提供建议,而且很可能我可以编辑我的两个问题,而不必仅仅因为我想删除我的问题而争论"并抱怨,现在这是不可能的,并且如果我发布其他问题,那么显然也会被标记为重复".这只是非常不幸.另外我也看不到- PDO提取仅返回第一行,因为此 PHP PDO Data_length的副本始终返回"0"

.. maybe what he should do is just advice in the comments and most probably I could have edited both of my questions, instead of having to "argue" and complain just because I want to delete my question, which is now impossible, and if I post another question, it will obviously get marked as a Duplicate too. This is just very unfortunate.Also I don't see this - PDO fetch returns only first row as a duplicate of this PHP PDO Data_length always returns "0"

原始问题:我正在使用以下代码建立与数据库的连接,获取Data_length索引列,并根据数据计算数据库大小.

ORIGINAL QUESTION:I'm using the following code to make a connection to the database, fetch the Data_length index column, and calculate the database size based on the data.

由于某些原因,PDO将始终返回"0",这是第一行中Data_length索引的值.无论我做什么,我只会得到第一行的索引.

For some reason PDO will always return "0", which is the value for the Data_length index in the first row. Whatever I do, I only get the first rows index.

数据库是MySQL,即引擎MyISAM.

The database is MySQL, the engine MyISAM.

PHP版本:5.5.38
MySQL版本:5.5.50

PHP Version: 5.5.38
MySQL Version: 5.5.50

更新1:好吧,因为这个问题被标记为与 this 重复,我被迫更改源代码.以下是新的源代码,该源代码可以在一个主机上正常工作,但仍仅返回另一行上的第一行.

UPDATE 1:Well, as this question was marked a duplicate from this one, I was forced to change the source code. The following is the new source code which is working fine on one hosting, but still returns only the first row on another.

// START GET DATABASE SIZE
    // Connect to database
    $conn = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);

    // Execute query
    $stmt = $conn->query('SHOW TABLE STATUS');

    // Get size from array
    $size = $stmt->fetch(PDO::FETCH_ASSOC)["Data_length"];

    // Set values to variables for use
    $decimals = 4;
    $mbytes = round($size/(1024*1024),$decimals);
    $kilobytes = round(($size / 1024) * 10);
    echo $kilobytes;
    // END GET DATABASE SIZE

旧的源代码:我已经从此答案复制了该代码,因为它被认为可以正常工作.由于信誉不佳,我无法在那发表评论.

The old source code:I have copied this code from this answer as it was accepted as working. I couldn't comment there as I don't have enough reputation.

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

<?php
try {
    error_reporting(-1);
    $host_name  = "my_host";
    $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>

推荐答案

添加while循环,

while($row= $sth->fetch( PDO::FETCH_ASSOC )){
   echo $row['your_field_name'];
}

或者您可以使用fetchAll

$row= $sth->fetchAll();
print_r($row);

这篇关于PDO提取仅返回第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:16
查看更多