User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 402,459 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,938 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser: Programming Forums
Views: 3680 | Replies: 15 | Solved
Reply
Join Date: Sep 2006
Location: NYC
Posts: 133
Reputation: sn4rf3r is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
sn4rf3r's Avatar
sn4rf3r sn4rf3r is offline Offline
Junior Poster

Re: help a newbie? mySQL find & replace

  #11  
Oct 12th, 2006
theres no loop, you are updating all records which have a userid in the comma separated list and match youre where/and statements. Try wrapping each value in quotes (if the columns are int you dont need to do this)
you could also try this
[php]
if (mysql_num_rows($query)) {
$ary = array();
foreach (mysql_fetch_assoc($query) as $key => $val) {
$ary[] = $val;
}
$ary = implode(',',$ary);
}
[/php]
Its doing the same thing in a different way. if that still fails, post the relevant code.
Last edited by sn4rf3r : Oct 12th, 2006 at 9:58 pm. Reason: fixed syntax
Reply With Quote  
Join Date: Aug 2006
Posts: 15
Reputation: kyleknapp is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
kyleknapp kyleknapp is offline Offline
Newbie Poster

Re: help a newbie? mySQL find & replace

  #12  
Oct 12th, 2006
let me see if I understand. In your previously suggested code, the
mysql_fetch_assoc() function is supposed to retrieve ALL the matching userid's, right?

and then the implode() function is supposed to lump them all together into a single variable ($ary) which, if I view it should look something like 21,45,71,101,131,137,168,229,343,375,380,386,429,439,467,576,618,664,822,830,858,919,920

then the `userid` IN (".$ary.")" clause of the UPDATE command would find any userid that matches anything in the "imploded" field.

Makes sense to me, so why didn't it work? my echo $ary, "<br>"; command should have caused something resembling 21,45,71,101,131,137,168,229,343,375,380,386,429,439,467,576,618,664,822,830,858,919,920 to appear on the screen, but only "21" appeared.

I actually have gotten it to work, though it's probably terribly clumsy (certainly not as elegant as your "foreach" function). My apparently functional hack job is as follows:
echo "selecting userids for 68104<br>";

$sql = 'SELECT userid FROM `phplist_user_user_attribute` WHERE `attributeid`= 7 AND `value` LIKE "99999"';
echo "querying for selected userids<br>";
$query = mysql_query($sql);
echo "setting attribute<br>";
echo $rows = mysql_num_rows($query), " rows selected<br>";

$i = 0;
while ($i < $rows) {
$i = $i+1;
if (mysql_num_rows($query)) {
$ary = implode(',', mysql_fetch_assoc($query));
echo $i, "   userid: ", $ary, "<br>";
$update = "UPDATE  `phplist_user_user_attribute`  SET  `value`  =  'on' WHERE  `attributeid`  ='16' AND  `userid` IN (".$ary.")";
mysql_query($update);
}} 

echo "done<br>";

yields the following display:
selecting userids for 68104
querying for selected userids
setting attribute
23 rows selected
1   userid: 21
2   userid: 45
3   userid: 71
4   userid: 101
5   userid: 131
6   userid: 137
7   userid: 168
8   userid: 229
9   userid: 343
10   userid: 375
11   userid: 380
12   userid: 386
13   userid: 429
14   userid: 439
15   userid: 467
16   userid: 576
17   userid: 618
18   userid: 664
19   userid: 822
20   userid: 830
21   userid: 858
22   userid: 919
23   userid: 920
done

and appears to be setting the "targeted" field as desired. What d'ya think?

~kyle
Reply With Quote  
Join Date: Sep 2006
Location: NYC
Posts: 133
Reputation: sn4rf3r is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
sn4rf3r's Avatar
sn4rf3r sn4rf3r is offline Offline
Junior Poster

Re: help a newbie? mySQL find & replace

  #13  
Oct 13th, 2006
you dont need to embed your if statement inside of your while loop, should be the other way around. Also you are creating multiple update queries when you only need one.

Try this:
[php]
if (mysql_num_rows($query)) {
while ($row = mysql_fetch_array($query)) {
$ary[] = $row;
}

echo '<pre>';
print_r($ary)
echo '</pre>';
$ary = implode(',',$ary);
echo 'imploded values ' . $ary;

$update = "update phplist_user_user_attribute set value = 'on' where attributeid = 16 and userid in (".$ary.")";
die($update);
}
[/php]

See what that shows?
Last edited by sn4rf3r : Oct 13th, 2006 at 1:08 pm. Reason: fixed syntax
Reply With Quote  
Join Date: Aug 2006
Posts: 15
Reputation: kyleknapp is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
kyleknapp kyleknapp is offline Offline
Newbie Poster

Re: help a newbie? mySQL find & replace

  #14  
Oct 13th, 2006
thanks, I'll take a look at that.

~kyle
Reply With Quote  
Join Date: May 2008
Location: indonesia
Posts: 5
Reputation: fanaticweb is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
fanaticweb fanaticweb is offline Offline
Newbie Poster

Re: help a newbie? mySQL find & replace

  #15  
May 1st, 2008
find and replace in mysql is very easy:

  1. UPDATE `table` SET fieldname=REPLACE(fieldname,'old,'new');
  2.  
Last edited by peter_budo : May 3rd, 2008 at 6:08 pm. Reason: Keep It Organized - please use [code] tags
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,227
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 10
Solved Threads: 270
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: help a newbie? mySQL find & replace

  #16  
May 3rd, 2008
What is point to bump post started 2 years ago, with last respond some 18 months back?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MySQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the MySQL Forum

All times are GMT -4. The time now is 3:46 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC