Jump to content

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

Edited by DSTR3

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

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.