LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-10-2008, 02:46 PM   #1
konqi
Member
 
Registered: Oct 2007
Distribution: Gentoo, Slackware
Posts: 162

Rep: Reputation: 16
php + sql: endless loop problem


What is wrong with this code? It gives an endless loop, but i don't see why.

Code:
<?php $sql = 'SELECT DISTINCT browser FROM stats';
      $query = mysql_query($sql);
      while ($browsers = mysql_fetch_assoc($query)) {
        $browser = $browsers['browser'];
        
        $sql = 'SELECT browser FROM stats WHERE browser="'.$browser.'"';
        $query = mysql_query($sql);
        $count = mysql_num_rows($query);
        $perc = $count / $visitors;
        echo $browser.' - '.$perc.'<br />';
      }
?>
 
Old 05-10-2008, 04:12 PM   #2
thexplorer
LQ Newbie
 
Registered: May 2008
Location: Brussels, Belgium
Distribution: fedora core 8
Posts: 1

Rep: Reputation: 0
Wink variable name

Hi !

Maybe, you should use a different name in the loop for the $query variable
because each time you make a mysql_fetch_assoc passing $query variable, the variable content has been modified inside the loop.
Hope this will help you.

Jeff
 
Old 05-11-2008, 02:23 AM   #3
konqi
Member
 
Registered: Oct 2007
Distribution: Gentoo, Slackware
Posts: 162

Original Poster
Rep: Reputation: 16
Yes it worked, but i used this name throughout the hole document. Can i do this, or should i chose another name for each entry?
 
Old 05-11-2008, 02:51 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
In the example that you gave the variable $query was used to assist with two tasks simultaneously. First the collection of the different browsers held on the stats table and then then to gather the number of rows there are on that table. Because you had used the same variable name for both tasks when it returned to the while condition it was looking at the reply from the second sql call not the first. That is why you need to have two distinct variable names.
 
Old 05-11-2008, 05:07 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
A separate issue is that you can achieve what you want to do with a single SQL statement. I think that it will be something like this
Code:
SELECT browser count(*) FROM stats GROUP BY browser
 
Old 05-11-2008, 10:23 AM   #6
konqi
Member
 
Registered: Oct 2007
Distribution: Gentoo, Slackware
Posts: 162

Original Poster
Rep: Reputation: 16
what does 'group by' mean?
 
Old 05-11-2008, 05:43 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Read the manual: http://dev.mysql.com/doc/refman/5.0/en/select.html
 
Old 05-11-2008, 09:02 PM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The "group by" will sort the result into groups, when it is used with aggregate functions, in this case count(*) the result is to give you the value of all the elements in that group, so by grouping by browser you will get the count of all the rows for each specific browser.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
FC2 startx endless loop zimzam Fedora 5 08-01-2005 01:02 PM
Hotplug endless loop AzCoder Ubuntu 4 04-30-2005 01:56 PM
Grub stuck in an endless loop. dikadika Linux - General 4 02-27-2005 07:46 PM
Mandrake 10.1 Install endless loop SteveI Mandriva 2 12-30-2004 09:56 AM
HELP- endless loop contrasutra Linux - Software 2 04-26-2003 11:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration