PDO阵列的结构不符合预期

时间:2018-07-13 19:12:57

标签: php arrays pdo

我正在通过PDO从数据库表中选择行,并尝试将其作为数组进行提取,以便可以使用foreach构建指向每个链接的链接列表。但是,它正在被奇怪地索引。

数据库中的行是:

NAME        | DESCRIPTION          | EFFECTIVE_TIMESTAMP | EXPIRATION_TIMESTAMP
-------------------------------------------------------------------------------
News Test   |Testing Page Library  |2018-07-13 00:00:00  |2018-07-14 00:00:00
Social Test |Testing Page Library2 |2018-07-13 00:00:00  |2018-07-14 00:00:00

但是,该数组当前如下所示:

Array ( [NAME] => News Test [0] => News Test [DESCRIPTION] => Testing page library [1] => Testing page library [EFFECTIVE_TIMESTAMP] => 2018-07-13 00:00:00[2] => 2018-07-13 00:00:00 [EXPIRATION_TIMESTAMP] => 2018-07-14 00:00:00 [3] => 2018-07-14 00:00:00 )
Array ( [NAME] => Social Test [0] => Social Test [DESCRIPTION] => Testing page library2 [1] => Testing page library2 [EFFECTIVE_TIMESTAMP] => 2018-07-13 00:00:00 [2] => 2018-07-13 00:00:00 [EXPIRATION_TIMESTAMP] => 2018-07-14 00:00:00 [3] => 2018-07-14 00:00:00 )

因此,似乎将每一行加倍,并对索引进行奇怪的索引。我只是想使用PDO来获取这两行,每一行都是按列索引的数组,因为我想回显页面上每一行的链接。

我应该如何更改提取和/或foreach循环以解决此问题?我从未见过这种情况。

getPages.php

$getContent = "SELECT * 
                FROM testSchema.page
                ";

try{
    $contentFetch = $DB2Conn->prepare($getContent);
    $contentResult = $contentFetch->execute();
    $fetchResult = $contentFetch->fetchAll();
} catch(PDOException $ex) {
    echo "QUERY FAILED: " .$ex->getMessage();
}

?>

Pages.php

<?php foreach($fetchResult as $page): ?>
<h2><?php print_r($page) ?></h2>  //This is what prints the incorrect array above
<?php endforeach?>

0 个答案:

没有答案