php - 将 mysql(PDO) 结果输出到 HTML 表的更好方法是什么

标签 php mysql html pdo

大约 3 周前开始学习 PHP,现在正在研究数据库连接和查询。我的问题涉及到一章编程教学点。尽管我无法测试我的代码,但我在此站点上所做的研究使我使用 while 循环编写了以下常规查询的代码。然而,这本书所解释和教导的东西是不同的。一种方法比另一种更好,还是两者都实现相同的目标,即从数据库中提取数据并输出到 HTML 表?

而且,我相信我在这里学到的东西比任何书上都多。意见和教导很有值(value)。

这就是我所拥有的:

<?php
        /set connection parameter variables
$hostname = 'localhost';
$username = 'richardweb';
$password = 'richardchocolate';
$dbname = 'richard_ricardo_assignment_db';

$conn = new PDO("mysql:dbname=$dbname;host=$hostname", $username, $password);

//set mysql query

$sql = 'SELECT studentID, name, email 
        FROM student
        ORDER BY studentID';

$qry = $conn->query($sql);

$qry->setFetchMode(PDO::FETCH_ASSOC);
    try {
        $conn = new PDO("mysql:dbname=$dbname;host=$hostname"; $username, $password);

        $sql = 'SELECT studentID, name, email 
                FROM student
                ORDER BY studentID';
        $qry = $conn->query($sql);
        $qry->setFetchMode(PDO::FETCH_ASSOC);
    }

    catch (PDOException $e) {
         echo "Error connecting to the database" . $e->getMessage();
    }
    ?>

    <!doctype html>
    <html>
    <head>
    <title>Whatever</title>
    <link rel="stylesheet" type="text/css" href="main.css">
    </head>
    <body>

    <div class="main">

    <h1>Students</h1>
    <table class="table1">
    <thead>
    <tr>
    <th>Student ID</th>
    <th>Name</th>
    <th>Email</th>
    </tr>
    </thead>
    <tbody>
    <?php while ($row = $qry->fetchALL(); ?>
    <tr>
    <td><?php echo htmlspecialchars($r['studentID'])?></td>
    <td><?php echo htmlspecialchars($r['name'])?></td>
    <td><?php echo htmlspecialchars($r['email'])?></td>
    </tr>
    <?php endwhile; ?>
    </tbody>
    </table>
    </div>
    </body>
    </html>

This is what my book covers for displaying results in HTML Table. Only showing the tabular data output. I'm only giving the example from the book and have not converted my code. 

<?php foreach ($products as $product) : ?>
<tr>
    <td><?php echo $product['productCode']; ?></td>
    <td><?php echo $product['productName']; ?></td>
    <td><?php echo $product['listPrice']; ?></td>
</tr>
<?php endforeach; ?>

最佳答案

如果您成功执行了 PDO 语句,有两种主要方法可以获取结果。

  1. 一次一个 - 这通常在 while 循环中完成,使用 PDOStatement 的方法之一从结果集中的一行中获取数据,例如 fetch()fetchColumn()fetchObject(),如下所示:

    while ($row = $stmt->fetch()) {
       // do something with data from each row
    }
    
  2. 一次性完成。为此,您将使用 fetchAll(),然后通常使用 foreach 循环来迭代行数组。

    $rows = $stmt->fetchAll();
    foreach ($rows as $row) {
        // do something with data from each row
    }
    

两者本质上并不比另一个更好。您应该选择哪一种取决于您需要对数据执行什么操作。对于简单创建 HTML 表来显示结果之类的情况,使用“一次一个”方法就足够了。

fetchAll() 的一个合理用途是,如果您需要在程序中的不同点对结果执行多个不同的操作。如果您使用“一次一个”方法,则需要重复执行查询,这通常效率较低。

但是在使用 fetchAll 时,请务必注意此警告(来自 PHP 文档):

Using this method to fetch large result sets will result in a heavy demand on system and possibly network resources. Rather than retrieving all of the data and manipulating it in PHP, consider using the database server to manipulate the result sets. For example, use the WHERE and ORDER BY clauses in SQL to restrict results before retrieving and processing them with PHP.

关于php - 将 mysql(PDO) 结果输出到 HTML 表的更好方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36342293/

相关文章:

php - Wordpress 基于用户代理改变主题

Mysql 安全转储和恢复

php - Mysql 数据透视表在 PhpMyadmin 中完全可以工作,但不能在 Php mysqli_query() 中工作

PHP 验证显示错误的图标

javascript - jQuery - 将 ASP.NET 部分 View 动态加载到模式中

php - 使用长脚本返回 502 Bad Gateway 错误

php - SELECT * FROM table_name ORDER BY column_name?

php - PHP 中的 Mysql 全文搜索返回多个值

php - SQL注入(inject)攻击——使用mysqli_multi_query()

javascript - 随机将类添加到两个div