php:是否有理由在回显数据之前先填充一个结果数组


php: is there a reason to first fill a result array before echoing the data

我正在阅读sitepoint的书PHP&MySQL新手到忍者,我想知道他为什么要多走一步。

我的SQL查询是

try
{
    $sql = 'SELECT * FROM trazooevents WHERE county = :county';
    $s = $pdo->prepare($sql);
    $s->bindValue(':county', $_POST['search_term']);
    $s->execute();
    include 'events.inc.php';
}

并且包括

<?php foreach ($s as $row): ?>
    <li class="db-headline"><?php echo htmlspecialchars($row['eventname'], ENT_QUOTES, 'UTF-8'); ?></li>
    // etc.
<?php endforeach; ?>

而且效果非常好。Kevin Yank在书中添加了一个类似于的额外步骤

foreach ($result as $row)
{
    $events[] = array(
    'eventname' => $row['eventname'],
    'eventdetails' => $row['eventdetails'],
    'weburl' => $row['weburl'],
    'imagename' => $row['imagename'],
    'expireson' => $row['expireson']
    );
}

他这样做只是为了得到一个索引标签,还是存在一些我不知道的安全问题?

这是代码可读性的问题。您不会使用行,而是使用一个更具语义意义的数组:events。

这只是为了获得索引标签。。不过没有必要..:)