PDO fetch returns only first row [duplicate]

天大地大妈咪最大 提交于 2019-12-20 07:51:07

问题


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:

"what you should have done, was edit your other question, rather than reposting with the same code. Sorry, but I won't be reopening the question. I posted a few comments under your other question yesterday but didn't bother replying so I ended up deleting them. Stating that there was nothing wrong with your code since I tested it."

.. 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”

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.

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.

The database is MySQL, the engine MyISAM.

PHP Version: 5.5.38
MySQL Version: 5.5.50

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>

回答1:


Add a while loop,

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

Or you can use fetchAll

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


来源:https://stackoverflow.com/questions/39935210/pdo-fetch-returns-only-first-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!