本文介紹了PDO fetch 只返回第一行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我使用以下代碼連接數(shù)據(jù)庫,獲取Data_length
索引列,并根據(jù)數(shù)據(jù)計算數(shù)據(jù)庫大小.
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.
數(shù)據(jù)庫為 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
這是源代碼.
<!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 . "
" . $row["Data_length"];
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
</body>
</html>
推薦答案
添加一個 while 循環(huán),
Add a while loop,
while($row= $sth->fetch( PDO::FETCH_ASSOC )){
echo $row['your_field_name'];
}
或者你可以使用 fetchAll
$rows = $sth->fetchAll();
print_r($rows);
這篇關(guān)于PDO fetch 只返回第一行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!