简体   繁体   English

如何从PHP中的数据库中获取所有数据

[英]How to fetch all the data from database in php

Here is my code: 这是我的代码:

<?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();
    }
?>

Now i am learning php, I want know about pdo connection to insert,update fetch data from database. 现在我正在学习php,我想了解有关pdo连接的信息,以更新数据库中的数据。 I referred this link http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#7.1 我引用了此链接http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#7.1

Now i got first column value.. May i know how to fetch all the records from database? 现在,我获得了第一列的值。我可以知道如何从数据库中获取所有记录吗?

Thanks in advance. 提前致谢。

You need to use 您需要使用

$result = $stmt->fetchAll();

rather then 而不是

$result = $stmt->fetch(PDO::FETCH_ASSOC);

You could just use ->fetchAll() method with this: 您可以使用->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
}

Sidenote: Its still an array inside, so you need to access the index of that copy inside the foreach. 旁注:它仍然是一个数组,因此您需要在foreach中访问该副本的索引。

Or also like this: 或者也这样:

while($row = $result->fetch(PDO::FETCH_ASSOC)) {
    echo $row['id'] . ' - ' . $row['col1'] . '<br/>';
}

You only have a couple of little mistakes. 您只有几个小错误。

The $stmt->fetch() command gets one result row at a time, so you need to run it in a loop and I am afraid a foreach will not work as that is used to process all items of an array. $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();
}
?>

ADDITIONAL EDIT: 其他编辑:

I dont know what your column names are as you have done a select * but lets assume your table has 2 columns 1 called id and one called custname . 我不知道您select *列名是什么,但是让我们假设您的表有2列,分别称为idcustname

You would do 你会做

   $sql = "SELECT * FROM sampletable";
   $stmt = $dbh->query($sql);

   while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
       echo $result['id'] . '-' . $result['custname'] . '<br>';
   }

If you change the fetch() statement like this you can get the results as an object rather than an array, I prefer the object->property syntax to the array syntax, but the result is very similiar. 如果像这样更改fetch()语句,您可以将结果作为对象而不是数组来获取,我更喜欢object-> property语法而不是array语法,但是结果非常相似。 Like so 像这样

   $sql = "SELECT * FROM sampletable";
   $stmt = $dbh->query($sql);

   while ($result = $stmt->fetch(PDO::FETCH_OBJ)) {
       echo $result->id . '-' . $result->custname . '<br>';
   }

You can use fetchAll() in order to get all the results from a query as the following . 您可以使用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