Reputation: 44066
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>";
}
here is the table returned
<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>
Upvotes: 0
Views: 143
Reputation: 723578
You appear to be printing multiple tables. I don't think this is what you intend though. You need to print the table's opening and closing tags, and the headings, outside of the loop. You should also call mysql_fetch_array()
in the loop and not just once.
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>";
You should also consider escaping the data in $click
, but I don't know what your data looks like so I'm not sure what to put in the area just between the while
and print
statements.
Upvotes: 1
Reputation: 94143
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!)
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;
}
and then when you iterate over $all_clicks
, each iteration will have a complete row.
Upvotes: 4
Reputation: 4260
mysql_fetch_array() returns rows, it looks like your foreach is looping over fields in a row not rows in a result set.
Upvotes: 1
Reputation: 17967
do a print_r($all_clicks)
and check the result is what you expect it to be.
you don't really need to use a foreach if there's only one result.
$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>";
Upvotes: 0
Reputation: 46692
You need to do it like this:
$query = "Select * from click_tracker";
$result = mysql_query($query);
while($click = mysql_fetch_assoc($result)) {
Upvotes: 0