while loop deleting fields? help!! :)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

for ($i = 0; $i < $searchtermscount; $++)

should be

for ($i = 0; $i < $searchtermscount; $i++)
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post by blade_922 »

yeh i edited my post when i realised that, check back on the last post on first page.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

try this...

Code: Select all

<?php
/**
 * Replace the terms in this array with the terms inside the comments you want to remove
 */
$searchterms = array('pharmacy', 'snots', 'booty', 'stinkbug', 'snaggletooth', 'footsmell', 'buttscratch', 'stupiduserentry');
$sql = "UPDATE `mos_akocomment` SET `comment` = '' WHERE 1=1";
foreach($searchterms as $key => $value) {
  $sql .= " OR `comment` LIKE '%$value%'";
}
echo $sql;
?>
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post by blade_922 »

hey ok i tried that n it didnt delete the comments that time either.

Code: Select all

<?php 
/** 
 * Replace the terms in this array with the terms inside the comments you want to remove 
 */ 
$searchterms = array('pharmacy', 'snots', 'booty', 'stinkbug', 'snaggletooth', 'footsmell', 'buttscratch', 'stupiduserentry'); 

$sql = "UPDATE `mos_akocomment` SET `comment` = '' WHERE 1=1"; 

foreach($searchterms as $key => $value) { 
  $sql .= " OR `comment` LIKE '%{$value}%'"; 
} 

echo $sql; 
?>
I tried it n didnt delete the comments, lol what did i get myself into, i thought php mysql was easy(ive got alot of learning to do) :p

So dya know anything else i can try
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you did remember to pass your query through mysql_query() right?
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post by blade_922 »

uumm, im not sure. Im kinda not to familiar to php n mysql yet, so if ya can show me how exactly i would do that, that would be great. :) thanks
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

Code: Select all

<?php
/**
 * Replace the terms in this array with the terms inside the comments you want to remove
 */
$searchterms = array('pharmacy', 'snots', 'booty', 'stinkbug', 'snaggletooth', 'footsmell', 'buttscratch', 'stupiduserentry');

$conditions = array();
foreach($searchterms as $value)
  $conditions[] = "`comment` LIKE '%{$value}%'";

$sql = "UPDATE `mos_akocomment` SET `comment` = '' WHERE ".implode(" OR ", $conditions);

//
// connect to the db server and select db
//

$result = mysql_query($sql);

?>
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post by blade_922 »

Ok hey thanks alot. Thatsworking,

That deletes out the comment in the comment field, but not the entire comment row. How can i make it delete the entire comment, like i began off to do.
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

Code: Select all

<?php
/**
 * Replace the terms in this array with the terms inside the comments you want to remove
 */
$searchterms = array('pharmacy', 'snots', 'booty', 'stinkbug', 'snaggletooth', 'footsmell', 'buttscratch', 'stupiduserentry');

$conditions = array();
foreach($searchterms as $value)
  $conditions[] = "`comment` LIKE '%{$value}%'";

$sql = "DELETE FROM `mos_akocomment` WHERE ".implode(" OR ", $conditions);

//
// connect to the db server and select db
//

$result = mysql_query($sql);

?>
Just use DELETE FROM instead of UPDATE ;)
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post by blade_922 »

cheers man thanks alot, ive been tryin to solve this 4 days. Im up here itill 1am doing this lol. Thanks again.
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

I've been writing the reply around 2am.. php is addictive 8O :P
Post Reply