Page 1 of 1

sql delete from foreach function

Posted: Sat Jun 30, 2012 5:58 pm
by exoskeleton
hi guys.. masters... i have this problem deleting more than 1 record. my desired outcome should be:

mysql_query("DELETE FROM employee WHERE emp_id IN (2,5)");

2 and 5 are from foreach function ....

is this possible?

mysql_query("DELETE FROM employee WHERE emp_id IN (" .
foreach ($values as $a)
{
$a;
--$counter;
if ($counter != 0)
{ . ',' . ; }
else { .')'. ; }} );

it wont work... im been working on this for 2 days... whew.. my brain aches... i just want to get the value from an array so i suppose to use foreach function for that and put it on the delete query but it wont work... please help... thank you in advance

Re: sql delete from foreach function

Posted: Mon Jul 02, 2012 5:47 am
by egami

Code: Select all

# Delete all records in a table..
DELETE FROM $table WHERE 1

# Delete select records from table..
DELETE FROM $table WHERE fieldname = 'Something'

To do it your way..

$counted = count($values);
$counts = 1;
$string = '';

foreach($values as $a) { 
  if ($counts < $counted) { 
    $string .= "$a,";
  } else { 
    $string .= "$a";
  }
}

DELETE FROM $table WHERE fieldname IN ($string)

 

Re: sql delete from foreach function

Posted: Thu Jul 05, 2012 5:51 am
by exoskeleton
sir egami.. thank you very much..this is very helpful ... more power to you sir..