PDO fetch returns only first row [duplicate]

2019-03-07 01:59发布

This question is an exact duplicate of:

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条回答
ゆ 、 Hurt°
2楼-- · 2019-03-07 02:44

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);
查看更多
登录 后发表回答