•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 330,021 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 4,699 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:
Views: 2859 | Replies: 15 | Solved
![]() |
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.
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
•
•
Join Date: Aug 2006
Posts: 15
Reputation:
Rep Power: 2
Solved Threads: 0
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
then the
Makes sense to me, so why didn't it work? my
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:
yields the following display:
and appears to be setting the "targeted" field as desired. What d'ya think?
~kyle
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,920then 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
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?
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
•
•
Join Date: May 2008
Location: indonesia
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 1
find and replace in mysql is very easy:
sql Syntax (Toggle Plain Text)
UPDATE `table` SET fieldname=REPLACE(fieldname,'old,'new');
Last edited by peter_budo : 9 Days Ago at 6:08 pm. Reason: Keep It Organized - please use [code] tags
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
DaniWeb Marketplace (Sponsored Links)
- Previous Thread: How do I edit a file in MySql please?
- Next Thread: Image Database



Linear Mode