Jump to content

Counting Then Deleting


DSTR3

Recommended Posts

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'] ."'");
 }
 }

Link to comment
https://forums.phpfreaks.com/topic/274308-counting-then-deleting/
Share on other sites

$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'] ."'");
  }
  }

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'] ."'");
  }
  }

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'] ."'");
  }
  }

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
		 )";

  On 2/11/2013 at 12:03 AM, Psycho said:

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.