Voodoo Jai Posted July 7, 2008 Share Posted July 7, 2008 I want to read a db tables content to find out how many times an entry occurs, if it is above a certain number then I want to stop the reading and continue on with something else. But If once all the vlaues have been read and are not above the set level then I wan to do something else. the condition is : a count value <11. if value >11 the stop loop and do somthing else. Do I get the number of rows to read and then use a FOR loop to read all the values until a condition is true. But doesnt this loop have to run through all the values and waste time. Or do I use a WHILE loop that checks for a condtion and if not true then continues to loop until a value is true or until all the values are tested. Read a db value count value = 0 if count >11 add to the count value read another db value else stop reading the db values and do something else end is this the solution or is there a better way. Many thanks VoodooJai Link to comment https://forums.phpfreaks.com/topic/113589-looping-dilema-which-one-for-or-while-or-both/ Share on other sites More sharing options...
kenrbnsn Posted July 7, 2008 Share Posted July 7, 2008 Assuming you're using MySQL, you can get the number of records containing a certain value in one statement and then decide what to do. No need for loops. Ken Link to comment https://forums.phpfreaks.com/topic/113589-looping-dilema-which-one-for-or-while-or-both/#findComment-583629 Share on other sites More sharing options...
gigas10 Posted July 7, 2008 Share Posted July 7, 2008 $qry = mysql_query("SELECT * FROM table01"); $row = mysql_num_rows($qry); gives you the number of rows in a table. Link to comment https://forums.phpfreaks.com/topic/113589-looping-dilema-which-one-for-or-while-or-both/#findComment-583643 Share on other sites More sharing options...
Voodoo Jai Posted July 7, 2008 Author Share Posted July 7, 2008 Arrrrrrrrrrrr so if I select all the record with the same value and count them if count >11 do something, else do something else. Yes I am reading from a MySql db. so would it be SELECT COUNT(value) FROM tableName WHERE value = 123abc; "A little further on" VoodooJai Sorry missed previos reply Link to comment https://forums.phpfreaks.com/topic/113589-looping-dilema-which-one-for-or-while-or-both/#findComment-583646 Share on other sites More sharing options...
discomatt Posted July 7, 2008 Share Posted July 7, 2008 Using COUNT(*) is supposed to be slightly faster, from what I've read. Link to comment https://forums.phpfreaks.com/topic/113589-looping-dilema-which-one-for-or-while-or-both/#findComment-583650 Share on other sites More sharing options...
gigas10 Posted July 7, 2008 Share Posted July 7, 2008 faster? it doesnt matter until you have over 100 million rows Link to comment https://forums.phpfreaks.com/topic/113589-looping-dilema-which-one-for-or-while-or-both/#findComment-583653 Share on other sites More sharing options...
Voodoo Jai Posted July 7, 2008 Author Share Posted July 7, 2008 Brilliant thank you all for your help VoodooJai Link to comment https://forums.phpfreaks.com/topic/113589-looping-dilema-which-one-for-or-while-or-both/#findComment-583658 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.