繁体   English   中英

如何从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列,分别称为idcustname

你会做

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM