[英]how to fetch all rows data from the database using while loop and stored fetch values in variables in php
[英]How to fetch all the data from database in php
這是我的代碼:
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = '';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=sample", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database <br />';
$sql = "SELECT * FROM sampletable";
$stmt = $dbh->query($sql);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
foreach($result as $key =>$val){
echo $key. '-' .$val.'<br />';
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
現在我正在學習php,我想了解有關pdo連接的信息,以更新數據庫中的數據。 我引用了此鏈接http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#7.1
現在,我獲得了第一列的值。我可以知道如何從數據庫中獲取所有記錄嗎?
提前致謝。
您需要使用
$result = $stmt->fetchAll();
而不是
$result = $stmt->fetch(PDO::FETCH_ASSOC);
您可以使用->fetchAll()
方法:
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); // returns all rows
foreach($result as $key => $row) {
echo $row['id'] . ' - ' . $row['column_name'] . '<br/>';
// ^ name of the column, this returns a multi dimensional
}
旁注:它仍然是一個數組,因此您需要在foreach中訪問該副本的索引。
或者也這樣:
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo $row['id'] . ' - ' . $row['col1'] . '<br/>';
}
您只有幾個小錯誤。
$stmt->fetch()
命令一次只能獲得一個結果行,因此您需要循環運行它,而且由於用於處理數組的所有項目,因此foreach恐怕不起作用。
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = '';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=sample", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database <br />';
$sql = "SELECT * FROM sampletable";
$stmt = $dbh->query($sql);
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
// you will have to change fieldname to an actual fieldname from the resultset
echo $result['fieldname'] . '<br />';
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
其他編輯:
我不知道您select *
列名是什么,但是讓我們假設您的表有2列,分別稱為id
和custname
。
你會做
$sql = "SELECT * FROM sampletable";
$stmt = $dbh->query($sql);
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $result['id'] . '-' . $result['custname'] . '<br>';
}
如果像這樣更改fetch()
語句,您可以將結果作為對象而不是數組來獲取,我更喜歡object-> property語法而不是array語法,但是結果非常相似。 像這樣
$sql = "SELECT * FROM sampletable";
$stmt = $dbh->query($sql);
while ($result = $stmt->fetch(PDO::FETCH_OBJ)) {
echo $result->id . '-' . $result->custname . '<br>';
}
您可以使用fetchAll()
來從查詢中獲取所有結果,如下所示。
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = '';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=sample", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database <br />';
$sql = "SELECT * FROM sampletable";
$stmt = $dbh->query($sql);
$result = $stmt->fetchAll(); //instead of fetch(PDO::FETCH_ASSOC)
foreach ($results as $key => $result) {
echo $key. '-' .$val.'<br />';
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.