为什么循环更多然后又一次...我错过了一些东西 [英] why does it loop more then once...am i missing something
问题描述
表中只有一条记录,所以为什么循环,就像我有5个表,每个表一个字母
There is only one record in the table so why does it loop like i have 5 table with one letter each
$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>
推荐答案
mysql_fetch_array
fetches one row as an array. When you try to loop over that result with your foreach
, you are actually looping through all the columns of the row you returned (twice, actually, because by default, mysql_fetch_array
returns an array with both numeric and indexed keys!)
如果要获取结果集中的所有行(并且很有可能),则需要使用while循环来不断获取行,直到不再存在行为止:
If you want to get all the rows in your result set (and you more than likely do), you need to use a while loop to keep fetching rows until there aren't anymore:
$all_clicks = array();
while ($row = mysql_fetch_array($result))
{
$all_clicks[] = $row;
}
,然后当您遍历 $ all_clicks
时,每次迭代都会有完整的一行.
and then when you iterate over $all_clicks
, each iteration will have a complete row.
这篇关于为什么循环更多然后又一次...我错过了一些东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!