Eamonn Posted January 8, 2008 Share Posted January 8, 2008 Ok I have about 40 mysql count(*) querys to execute and then perform some basic maths on the results. Here is the query "select count(*) from entries where lcase(smsc) like '%vodafone%' and logdate >='2007-12-01' and logdate <='2007-12-31' and action='Receive' and to_number in (55555,55556,55557,55558,55558) group by to_number;" This gives the following results when executed from command line mysql. +----------+ | count(*) | +----------+ | 123 | | 10 | | 25878 | | 1101 | | 1849 | +----------+ What I would like to end up with is a variable with the name of the to_number which contains the value of the result like this: $55555 = 123; $55556 = 10; $55557 = 25878; etc etc My guess is it should be some combination of using nested foreach loops or maybe writing a function that an array is looped through. Im still very new to php so I would be very grateful for any help. Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/ Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 Try using a Fetch http://us3.php.net/mysql_fetch_array Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/#findComment-433952 Share on other sites More sharing options...
nikefido Posted January 8, 2008 Share Posted January 8, 2008 can you make the first character of a variable a number in PHP? also, popular technique for looping through result sets are using a WHILE loop and mysql_fetch_array() Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/#findComment-433953 Share on other sites More sharing options...
Eamonn Posted January 8, 2008 Author Share Posted January 8, 2008 I've attempted to use fetch but it only seems to return the first result (123) I cant get any of the other results out :/ Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/#findComment-433958 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 You need to do the fetch in a while loop. while ($row = mysql_fetch_array($result)) Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/#findComment-433962 Share on other sites More sharing options...
nikefido Posted January 8, 2008 Share Posted January 8, 2008 while($counter = mysql_fetch_array($result_set) { echo $counter['count(*)'] } edit: what he said ^^ Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/#findComment-433963 Share on other sites More sharing options...
GingerRobot Posted January 8, 2008 Share Posted January 8, 2008 You shouldn't need to do any nesting. Just select the to_number along with the count. You'll also need to use an array, rather than the variables. A variable name cannot start with a number, so we cant have $55555. Try: <?php $sql = "select count(*),to_number from entries where lcase(smsc) like '%vodafone%' and logdate >='2007-12-01' and logdate <='2007-12-31' and action='Receive' and to_number in (55555,55556,55557,55558,55558) group by to_number;" $result = mysql_query($sql) or die(mysql_error()); $nums[] = array() while(list($count,$num) = mysql_fetch_row($result)){ $nums[$num] = $count; } echo $nums[55555]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/#findComment-433965 Share on other sites More sharing options...
Eamonn Posted January 8, 2008 Author Share Posted January 8, 2008 Thanks thats half way to what I need to do Instead of echo'ing the result I need to turn it into a variable is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/#findComment-433969 Share on other sites More sharing options...
kenrbnsn Posted January 8, 2008 Share Posted January 8, 2008 One solution: <?php $q = "select to_number, count(*) as my_count from entries where lcase(smsc) like '%vodafone%' and logdate >='2007-12-01' and logdate <='2007-12-31' and action='Receive' and to_number in (55555,55556,55557,55558,55558) group by to_number"; $rs = mysql_query($q); $counts = array(); while ($rw = mysql_fetch_assoc($rs)) { $counts[$rw['to_number']] = $rw ['my_count']; echo '<pre>' . print_r($counts,true) . '</pre>'; // debug ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/#findComment-433973 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 It is a variable... Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/#findComment-433974 Share on other sites More sharing options...
GingerRobot Posted January 8, 2008 Share Posted January 8, 2008 Did you read what i said? We're using an array, because variable names cannot start with a number. I added an echo at the end as an example Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/#findComment-433975 Share on other sites More sharing options...
Eamonn Posted January 8, 2008 Author Share Posted January 8, 2008 Ginger: I tried that code but it doesnt work.. just gives me a blank page. But from reading through it your defo on the right track. I even changed echo $nums[55555]; to echo $nums['55555']; but that didnt work either. Any ideas? Thanks a million to everyone who has posted so far this has been melting my brain for days Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/#findComment-434001 Share on other sites More sharing options...
GingerRobot Posted January 8, 2008 Share Posted January 8, 2008 Somehow i managed to miss off three semi-colons in that post. I noticed one and modified, but didn't see the other two. I must be tired! Try: <?php $sql = "select count(*),to_number from entries where lcase(smsc) like '%vodafone%' and logdate >='2007-12-01' and logdate <='2007-12-31' and action='Receive' and to_number in (55555,55556,55557,55558,55558) group by to_number;"; $result = mysql_query($sql) or die(mysql_error()); $nums[] = array(); while(list($count,$num) = mysql_fetch_row($result)){ $nums[$num] = $count; } echo $nums[55555]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/#findComment-434012 Share on other sites More sharing options...
Eamonn Posted January 8, 2008 Author Share Posted January 8, 2008 GingerRobot you absolute legend!!!! Thank you so much for helping me I have been so frustrated by this for days. Thanks!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/85087-how-to-loop-through-mysql-results/#findComment-434020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.