1

我正在尝试将我的 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] => )

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

4

1 回答 1

2

读取mysql_fetch_array

关于它被弃用的部分很有趣,但您可能想查看result_type参数并通过MYSQL_ASSOC

有一个等效(也已弃用)函数可以在一次调用中执行此操作:

mysql_fetch_assoc

于 2013-09-20T21:55:28.083 回答