2010-09-08 64 views
0

没有在表中只有一条记录为何还循环像我有5台用一个字母每个为什么它循环,曾多次......我思念的东西

$query = "Select * from click_tracker"; 
$result = mysql_query($query); 
$all_clicks = mysql_fetch_array($result); 

foreach($all_clicks as $click){ 
    print " 
    <table border=\"1\"> 
     <tr> 
     <th>Location</th> 
     <th>Visit Count</th> 
     </tr> 
     <tr> 
     <td>{$click['url_destination']}</td> 
     <td>{$click['count']}</td> 
     </tr> 
    </table>"; 
} 

这里是表返回

<table border="1"> 
    <tr> 
    <th>Location</th> 

    <th>Visit Count</th> 
    </tr> 
    <tr> 
    <td>2</td> 
    <td>2</td> 
    </tr> 
</table> 

<table border="1"> 
    <tr> 
    <th>Location</th> 
    <th>Visit Count</th> 
    </tr> 
    <tr> 
    <td>2</td> 

    <td>2</td> 
    </tr> 
</table> 
<table border="1"> 
    <tr> 
    <th>Location</th> 
    <th>Visit Count</th> 

    </tr> 
    <tr> 
    <td>h</td> 
    <td>h</td> 
    </tr> 
</table> 
<table border="1"> 
    <tr> 

    <th>Location</th> 
    <th>Visit Count</th> 
    </tr> 
    <tr> 
    <td>h</td> 
    <td>h</td> 
    </tr> 

</table> 
<table border="1"> 
    <tr> 
    <th>Location</th> 
    <th>Visit Count</th> 
    </tr> 
    <tr> 
    <td>5</td> 

    <td>5</td> 
    </tr> 
</table> 
<table border="1"> 
    <tr> 
    <th>Location</th> 
    <th>Visit Count</th> 

    </tr> 
    <tr> 
    <td>5</td> 
    <td>5</td> 
    </tr> 
</table> 

回答

4

mysql_fetch_array提取一个行作为数组。当您尝试遍历该结果与你的foreach,你实际上是通过所有的列你返回的行循环(两次,其实,因为默认情况下,mysql_fetch_array返回与数字和索引键的排列!)

如果你想获得所有在结果集的行(和你更有可能这样做),你需要使用一个while循环,以保持读取行,直到有不再:

$all_clicks = array(); 
while ($row = mysql_fetch_array($result)) 
{ 
    $all_clicks[] = $row; 
} 

然后当你迭代$all_clicks时,每次迭代将有一个完整的行。

+1

+1对于有用的描述acutal问题(即列) – Ross 2010-09-08 18:19:14

0

你需要做的是这样的:

$query = "Select * from click_tracker"; 
$result = mysql_query($query); 
while($click = mysql_fetch_assoc($result)) { 
+0

我相信mysql_fetch_array会返回一个数组,因此$ all_clicks是一个数组 - $ result是一个结果资源。 – Ross 2010-09-08 18:13:15

+0

Uhm ... PHP手册中提到了mysql_fetch_array()“返回一个与获取的行对应的数组,并将内部数据指针向前移动。”在示例中,'$ result'是一个资源,而不是'$ all_clicks' – Robin 2010-09-08 18:13:47

+0

你的代码在你编辑你的答案之前已经读了'mysql_fetch_assoc($ all_clicks)'几秒钟。 – BoltClock 2010-09-08 18:25:41

0

print_r($all_clicks)并检查结果是你期待什么它是。

如果只有一个结果,您并不需要使用foreach。

$query = "Select * from click_tracker"; 
    $result = mysql_query($query); 
    $all_clicks = mysql_fetch_array($result); 
    print " 
     <table border=\"1\"> 
      <tr> 
      <th>Location</th> 
      <th>Visit Count</th> 
      </tr> 
      <tr> 
      <td>{$all_clicks['url_destination']}</td> 
      <td>{$all_clicks['count']}</td> 
      </tr> 
     </table>"; 
1

mysql_fetch_array()返回行,它看起来像你的foreach循环是在田成一排的结果集不是行。

1

您似乎在打印多个表格。我不认为这是你想要的。您需要在循环外打印表格的开始和结束标签以及标题。你也应该在循环中调用mysql_fetch_array(),而不是一次。

print " 
    <table border=\"1\"> 
     <tr> 
     <th>Location</th> 
     <th>Visit Count</th> 
     </tr>"; 

$query = "Select * from click_tracker"; 
$result = mysql_query($query); 

while ($click = mysql_fetch_array($result)) { 
    print " 
     <tr> 
     <td>{$click['url_destination']}</td> 
     <td>{$click['count']}</td> 
     </tr>"; 
} 

print "</table>"; 

你也应该考虑在$click逃逸的数据,但我不知道你的数据是什么样子,所以我不知道要放什么东西在该地区刚刚whileprint语句之间。

相关问题