DSTR3 Posted February 10, 2013 Share Posted February 10, 2013 I am having problems with the syntax on this. I am counting the number of LocationID's in the tblTemp and if they are less than a certain number I delete them. But my syntax is off. $sql = "SELECT LocationID, count(LocationID) AS totalCount FROM tblTemp GROUP BY LocationID"; $res = mysql_query($sql) or die("FAIL: $sql <br>" . mysql_error()); foreach($row = mysql_fetch_array($res)) { if($row['totalCount'] < (" . $nchecked . ")) { } $mysql_query2("DELETE FROM tblTemp WHERE LocationID = '". $row['LocationID'] ."'"); } } Quote Link to comment https://forums.phpfreaks.com/topic/274308-counting-then-deleting/ Share on other sites More sharing options...
requinix Posted February 10, 2013 Share Posted February 10, 2013 if($row['totalCount'] < (" . $nchecked . ")) { I don't know what you're doing with $nchecked but I'm quite sure you should be using if($row['totalCount'] < $nchecked) { Quote Link to comment https://forums.phpfreaks.com/topic/274308-counting-then-deleting/#findComment-1411593 Share on other sites More sharing options...
DSTR3 Posted February 10, 2013 Author Share Posted February 10, 2013 (edited) $nchecked is a variable that counts how many options I have selected. Then I am deleting against that value. I will try your suggestion. Thank you. Its still complaining about the foreach line. $result = mysql_query("SELECT LocationID, count(LocationID) as totalCount from tblTemp Group BY LocationID") foreach($row = mysql_fetch_array($result)) { if($row['totalCount'] < $nchecked) { } $mysql_query2("DELETE FROM tblTemp WHERE LocationID = '". $row['LocationID'] ."'"); } } Edited February 10, 2013 by DSTR3 Quote Link to comment https://forums.phpfreaks.com/topic/274308-counting-then-deleting/#findComment-1411597 Share on other sites More sharing options...
requinix Posted February 10, 2013 Share Posted February 10, 2013 "Still"? Alright, the whole thing then. 1. Missing a semicolon on the mysql_query() line. 2. foreach is for arrays, not conditions. Use a while loop. 3. I doubt mysql_query2 is a function variable. Quote Link to comment https://forums.phpfreaks.com/topic/274308-counting-then-deleting/#findComment-1411600 Share on other sites More sharing options...
DSTR3 Posted February 10, 2013 Author Share Posted February 10, 2013 Bingo! The errors are gone! But..................of course it's not deleting! $result = mysql_query("SELECT LocationID, count(LocationID) as totalCount from tblTemp Group BY LocationID"); while($row = mysql_fetch_array($result)) { if($row['totalCount'] < $nchecked) { } $mysql_query("DELETE FROM tblTemp WHERE LocationID = '". $row['LocationID'] ."'"); } } Quote Link to comment https://forums.phpfreaks.com/topic/274308-counting-then-deleting/#findComment-1411605 Share on other sites More sharing options...
Jessica Posted February 10, 2013 Share Posted February 10, 2013 You need to check for errors. Quote Link to comment https://forums.phpfreaks.com/topic/274308-counting-then-deleting/#findComment-1411607 Share on other sites More sharing options...
DSTR3 Posted February 10, 2013 Author Share Posted February 10, 2013 I did this and the ERROR I received was "Fatal error: Function name must be a string" on the last line. $result = mysql_query("SELECT LocationID, count(LocationID) as totalCount from tblTemp Group BY LocationID"); $result = mysql_query($sql) or die("FAIL: $sql <br>" . mysql_error()); while($row = mysql_fetch_array($result)) { if($row['totalCount'] < $nchecked) { } $mysql_query("DELETE FROM tblTemp WHERE LocationID = '". $row['LocationID'] ."'"); } } Quote Link to comment https://forums.phpfreaks.com/topic/274308-counting-then-deleting/#findComment-1411609 Share on other sites More sharing options...
Jessica Posted February 10, 2013 Share Posted February 10, 2013 Well? You have a variable where you should have a function. You're also not checking for MYSQL errors. Quote Link to comment https://forums.phpfreaks.com/topic/274308-counting-then-deleting/#findComment-1411610 Share on other sites More sharing options...
requinix Posted February 10, 2013 Share Posted February 10, 2013 3. I doubt mysql_query2 is a function variable. You probably mean to use mysql_query(). Quote Link to comment https://forums.phpfreaks.com/topic/274308-counting-then-deleting/#findComment-1411620 Share on other sites More sharing options...
Psycho Posted February 11, 2013 Share Posted February 11, 2013 Why are you running two queries: 1 to get the count and another to delete. And the delete query is in a loop no less. Never run queries in loops. All you need is ONE query $query = "DELETE FROM tblTemp WHERE LocationID IN ( SELECT LocationID FROM (SELECT LocationID FROM tblTemp GROUP BY LocationID HAVING count(*) < $nchecked ) t )"; Quote Link to comment https://forums.phpfreaks.com/topic/274308-counting-then-deleting/#findComment-1411622 Share on other sites More sharing options...
DSTR3 Posted February 11, 2013 Author Share Posted February 11, 2013 Thank you. I see the light. Quote Link to comment https://forums.phpfreaks.com/topic/274308-counting-then-deleting/#findComment-1411625 Share on other sites More sharing options...
Jessica Posted February 11, 2013 Share Posted February 11, 2013 Why are you running two queries: 1 to get the count and another to delete. And the delete query is in a loop no less. Never run queries in loops. All you need is ONE query $query = "DELETE FROM tblTemp WHERE LocationID IN ( SELECT LocationID FROM (SELECT LocationID FROM tblTemp GROUP BY LocationID HAVING count(*) < $nchecked ) t )"; I was going to point it out once we solved the basic debugging issues. But do you even need the subselect? Quote Link to comment https://forums.phpfreaks.com/topic/274308-counting-then-deleting/#findComment-1411628 Share on other sites More sharing options...
Psycho Posted February 11, 2013 Share Posted February 11, 2013 But do you even need the subselect? I wouldn't think so, but I couldn't get it working otherwise. Perhaps Barand can provide the more efficient solution. Quote Link to comment https://forums.phpfreaks.com/topic/274308-counting-then-deleting/#findComment-1411801 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.