SQL查询结果中的双列

时间:2013-09-20 21:33:36

标签: php mysql sql html-table

我正在尝试将SQL查询直接输出到表中(不将结果分配给变量)。这就是我所拥有的:

<?php
$query = 'SELECT * FROM sites';
$result = mysql_query($query);

echo '<table>';

$count = 0;

while($row = mysql_fetch_array($result)) {

    echo '<tr>';
    $count = $count + 1;
    if($count == 1) {                //Header row
        foreach($row as $name => $value) {
            echo '<th>' . $name . '</th>';
        }
        echo '</tr/><tr>';
    }

    foreach($row as $name => $value) {   //Data rows
        echo '<td>' . $value . '</td>';
    }
    echo '</tr>';
}
echo '</table>';

然而,在结果中我得到了双列,如下所示:

0 | ID | 1 | URL | 2 | startDate | etc
1 | 1  | a | a   | b | b         | etc

以下是几个示例行:

Array ( [0] => 1 [ID] => 1 [1] => dadf [URL] => dadf
        [2] => asdf [startDate] => asdf [3] => [server] =>
        [4] => asdf [paymentStatus] => asdf
        [5] => [hostingType] => [6] => [hostingPlan] => ) 

Array ( [0] => 2 [ID] => 2 [1] => hihi [URL] => hihi.com
        [2] => [startDate] => [3] => [server] =>
        [4] => [paymentStatus] => [5] => [hostingType] =>
        [6] => [hostingPlan] => )

我不知道这些编号的重复列来自何处。当然,它们不存在于数据库中,但它们位于结果行中。也许有一种完全不同的方式来做我想做的事情?

1 个答案:

答案 0 :(得分:2)

阅读mysql_fetch_array

关于它被弃用的部分很有意思,但您可能希望查看result_type参数并传递MYSQL_ASSOC

在一次调用中有一个等效(也已弃用)的功能:

mysql_fetch_assoc