Problem with either my while loop or my syntax. [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nightswore
Forum Newbie
Posts: 13
Joined: Thu May 15, 2008 11:26 am

Problem with either my while loop or my syntax. [Solved]

Post by nightswore »

So I have a table of data that reads : Name | Date | Count. And what I'm trying to do is that every time a name comes up, I increase it's count by 1 and increase all other occurrences of the name's count by 1. So say Joe comes up, I want every instance of Joe to have its count increased by 1 so that no matter where I come across his name on the list, I know that Joe is on there X amount of times. To do this I employ this little while loop:

Code: Select all

while($rows = mysql_fetch_array($search))
{
    echo $rows['date'] . " " . $rows['name']. " " . $rows['count'];
      $increment = $rows['count'];
      $increment = $increment +1;
      $names = $rows['name'];
 
mysql_query("UPDATE temp SET count = '$increment' WHERE
name ='$names'");
 
}
The echo is really just for testing purposes as this page redirects at the bottom after it is finished working. The while loop takes the number that is currently in the count for the name and then increases it by 1 and then updates the count on the database.

Is there a problem with my update that it is only affecting the current instance of each name? Right now when I print out the table after implementing this loop each count is only at 1.

If someone can find an obvious flaw in this I would appreciate it because I've been staring at it for a while now and I'm starting to see it on the backs of my eyelids.

Thanks. -Z
Last edited by nightswore on Wed Jun 04, 2008 1:18 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Problem with either my while loop or my syntax.

Post by RobertGonzalez »

Code: Select all

<?php
while ($row = mysql_fetch_array($search)) {
  mysql_query("UPDATE `temp` SET `count` = `count` + 1 WHERE name ='$row[name]'"); 
}
?>
nightswore
Forum Newbie
Posts: 13
Joined: Thu May 15, 2008 11:26 am

Re: Problem with either my while loop or my syntax.

Post by nightswore »

A godsend you are, thanks!

-Z
Post Reply