Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

sql delete from foreach function

Links for php scripts

Moderators: macek, egami, gesf

sql delete from foreach function

Postby exoskeleton » Sat Jun 30, 2012 5:58 pm

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
exoskeleton
New php-forum User
New php-forum User
 
Posts: 7
Joined: Mon Jun 25, 2012 1:21 am

Re: sql delete from foreach function

Postby egami » Mon Jul 02, 2012 5:47 am

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)

 
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: sql delete from foreach function

Postby exoskeleton » Thu Jul 05, 2012 5:51 am

sir egami.. thank you very much..this is very helpful ... more power to you sir..
exoskeleton
New php-forum User
New php-forum User
 
Posts: 7
Joined: Mon Jun 25, 2012 1:21 am


Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.