【问题标题】:PHP array isn't displaying in pagePHP数组未显示在页面中
【发布时间】:2026-01-25 12:25:01
【问题描述】:

笔记列表应该显示在 ul li 跨度内,有什么理由不显示它们,而是数组显示在页面顶部?

数据库连接似乎工作得很好,但是注释没有显示在跨度内。它还删除了“您尚未添加任何注释文本”

代码

<?php

require_once 'app/init.php';

$notesQuery = $db->prepare("
    SELECT ID, note
    FROM notes
");

$notesQuery->execute();

$notes = $notesQuery->rowCount() ? $notesQuery : [];

foreach($notes as $note) {
    print_r($note);
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notes</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <header>
        <h1>myNotes</h1>
        <nav>
            <a href="index.php">Home</a>
            <a href="about.html">About</a>
            <a href="contact.html">Contact</a>
        </nav>
    </header> 
    <div class="container">
        <form action="add.php" method="post"> 
                <textarea name="note" placeholder="Insert a note..." autocomplete="off" required></textarea>
                <input type="submit" value="Add" />
        </form>
        <div class="notes">
            <h2>Notes</h2>

            <?php if(!empty($notes)): ?>

            <ul>
                <?php foreach($notes as $note): ?>
                    <li>
                        <span><?php echo $note['note']; ?></span>
                    </li>
                <?php endforeach; ?>
            </ul>
                <?php else: ?>
                    <p>you haven't added any notes yet.</p>
                <?php endif; ?>

        </div>

【问题讨论】:

  • print_r($notes); foreach 循环之前。
  • 对不起,我会把这个放在哪里?如果我把它放在&lt;?phpforeach($notes as$note): ?&gt; 之前,我会收到`PDOStatement Object ([queryString] => SELECT ID, note FROM notes)`
  • 您已经执行了语句,但您还没有fetched 行。
  • $notes 是什么类型的对象?这是哪个数据库访问库?我很惊讶您可以在查询上运行一个循环而不调用其他方法来提取结果?看起来 $notes 不是数组,更像是查询结果集的迭代器,只能使用一次
  • No 如果查询返回 5 行则 $notes = 5。php.net/manual/en/pdostatement.rowcount.php

标签: php html pdo


【解决方案1】:

请随意使用以下示例进行查询。

// Your sql query
$sql  = "SELECT ID, note FROM notes";

// Prepare your statement
$stmt = $db -> prepare($sql);

// Execute your prepared statement
$stmt -> execute();

// Retreive all rows
$notes = $stmt -> fetchAll();

// Check if array is not empty
if (!empty($notes)) {
    //Spit out the array
    print_r($notes);
}

【讨论】:

    【解决方案2】:

    工作代码

    <?php
    
    require_once 'app/init.php';
    
    $notesQuery = $db->prepare("
        SELECT ID, note
        FROM notes
    ");
    
    $notesQuery->execute();
    
    $notes = $notesQuery->rowCount() ? $notesQuery : [];
    
    
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Notes</title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <header>
            <h1>myNotes</h1>
            <nav>
                <a href="index.php">Home</a>
                <a href="about.html">About</a>
                <a href="contact.html">Contact</a>
            </nav>
        </header> 
        <div class="container">
            <form action="add.php" method="post"> 
                    <textarea name="note" placeholder="Insert a note..." autocomplete="off" required></textarea>
                    <input type="submit" value="Add" />
            </form>
            <div class="notes">
                <h2>Notes</h2>
    
                <?php if(!empty($notes)): ?>
    
                <ul>
                    <?php foreach($notes as $note): ?>
                        <li>
                            <span><?php echo $note['note']; ?></span>
                        </li>
                    <?php endforeach; ?>
                </ul>
                    <?php else: ?>
                        <p>you haven't added any notes yet.</p>
                    <?php endif; ?>
            </div>
        </div>
    </body>
    </html>
    

    【讨论】: