【发布时间】:2013-09-26 09:34:54
【问题描述】:
我正在尝试将我的 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] => )
我不知道这些编号的重复列是从哪里来的。当然,它们不存在于数据库中,但它们存在于结果行中。也许有一种完全不同的方式来做我想做的事情?
【问题讨论】:
-
这似乎是
PHP。对吗? -
尝试使用
mysql_fetch_assoc而不是mysql_fetch_array
标签: php mysql sql html-table