使用PDO从mysql中提取多行数据并在网页表格式中进行格式化

时间:2021-06-29 17:00:19

I'm struggling to extract data from a mysql database and present it in a table on a webpage. I'm fairly sure I'm missing something simple but I'm new to this stuff and can't work out what's gone wrong. Here's hoping someone can spot my silly error and let me know!

我正在努力从mysql数据库中提取数据并将其呈现在网页上的表格中。我很确定我错过了一些简单的东西,但我对这些东西不熟悉并且无法弄清楚出了什么问题。这里希望有人能发现我的愚蠢错误让我知道!

Code prints the header info in the table OK but i get no rows.

代码打印表格中的标题信息确定,但我没有行。

When I've figured it out I plan to strip off the <html>, <head> and <body> tags and use an include on some other pages.

当我弄明白时,我计划剥离,和标签,并在其他一些页面上使用include。

SteveW

Code below

<!DOCTYPE html>
<html>
<?php
//database login info
require_once 'dbconfig.php';



try {
//connect to batabase
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
//check connection to database. It works OK
    echo "Connected to database $dbname at $host successfully. <br>";

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = ("SELECT `name`, `comment`, `entered` FROM `comment`");  
        $result = $pdo->query($sql);
    }
    //connection error
    catch (PDOException $pe) {
    die("Could not connect to the database $dbname :" . $pe->getMessage());
    }
?>
<head></head>
<body>
    <div id="container">
        <h1>Comments</h1>
        <table width ="100%" border ="1">
        <thead>
            <tr>
                <td>Name</td>
                <td>Comment</td>
                <td>Entered</td>
            </tr>
        </thead>
        <tbody>
        <?php while ($row = $result->FetchALL(PDO::FETCH_ASSOC)) {

            echo
            "<tr>   <td>".$row['comment']." </td>                   
                    <td>".$row['name']." </td>
                    <td>".$row['entered']." </td>
            </tr>\n";
            }
        $pdo->close;
        ?>
        </tbody>
        </table>    
</body>         
</html> 

1 个解决方案

#1


1  

$pdo->query()

returns a PDOStatement object, or FALSE on failure.

返回PDOStatement对象,失败时返回FALSE。

S0 no need fetch it twice $result->FetchALL(PDO::FETCH_ASSOC)

S0无需获取两次$ result-> FetchALL(PDO :: FETCH_ASSOC)

Just use foreach loop as

只需使用foreach循环

 foreach ($result as $row) {
           echo
            "<tr>   <td>".$row['comment']." </td>                   
                    <td>".$row['name']." </td>
                    <td>".$row['entered']." </td>
            </tr>\n";
            }

#1


1  

$pdo->query()

returns a PDOStatement object, or FALSE on failure.

返回PDOStatement对象,失败时返回FALSE。

S0 no need fetch it twice $result->FetchALL(PDO::FETCH_ASSOC)

S0无需获取两次$ result-> FetchALL(PDO :: FETCH_ASSOC)

Just use foreach loop as

只需使用foreach循环

 foreach ($result as $row) {
           echo
            "<tr>   <td>".$row['comment']." </td>                   
                    <td>".$row['name']." </td>
                    <td>".$row['entered']." </td>
            </tr>\n";
            }