tags:

views:

83

answers:

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>";
}

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>   
A: 

You need to do it like this:

$query = "Select * from click_tracker";
$result = mysql_query($query);
while($click = mysql_fetch_assoc($result)) {
shamittomar
i believe mysql_fetch_array returns an array, hence $all_clicks is an array - $result is a result resource.
Ross
Uhm... The PHP manual says of mysql_fetch_array() "Returns an array that corresponds to the fetched row and moves the internal data pointer ahead." In the example `$result` is a resource, not `$all_clicks`
Robin
@shamittomar: your code had read `mysql_fetch_assoc($all_clicks)` a couple of seconds before you edited your answer.
BoltClock
Ohh that. Yes, that was a typo. Now, I got what @Robin meant. Thanks for clearing :) It's been a long day and midnight here :)
shamittomar
A: 

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>";
Ross
+1  A: 

mysql_fetch_array() returns rows, it looks like your foreach is looping over fields in a row not rows in a result set.

Robin
+4  A: 

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.

Daniel Vandersluis
+1 for useful description of the acutal problem (i.e the columns)
Ross
+1  A: 

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.

BoltClock