[英]PDO fetch returns only first row
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.我正在使用以下代码连接到数据库,获取Data_length
索引列,并根据数据计算数据库大小。
For some reason PDO will always return "0", which is the value for the Data_length
index in the first row.出于某种原因,PDO 将始终返回“0”,这是第一行中Data_length
索引的值。 Whatever I do, I only get the first rows index.无论我做什么,我都只得到第一行索引。
The database is MySQL, the engine MyISAM.数据库是 MySQL,引擎 MyISAM。
PHP Version: 5.5.38 PHP 版本:5.5.38
MySQL Version: 5.5.50 MySQL 版本:5.5.50
Here is the source code.这是源代码。
<!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>
Add a while loop,添加一个while循环,
while($row= $sth->fetch( PDO::FETCH_ASSOC )){
echo $row['your_field_name'];
}
Or you can use fetchAll
或者你可以使用fetchAll
$rows = $sth->fetchAll();
print_r($rows);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.