Canman2005 Posted July 5, 2010 Share Posted July 5, 2010 Hi all I have the following simple code mysql_query("UPDATE `people` SET `access` = '".rand(11111,99999)."'"); But at the moment each row gets the same "rand()" code, is there anyway to assign each row a different "rand()"? Thanks Link to comment https://forums.phpfreaks.com/topic/206734-update-multiple-rows-with-random-code/ Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 Unless you have a "where" clause on the query, this Update query will set all the rows to have the same value in the access field. Using the "where" clause will restrict the update to rows that match the "where" condition. Ken Link to comment https://forums.phpfreaks.com/topic/206734-update-multiple-rows-with-random-code/#findComment-1081150 Share on other sites More sharing options...
Canman2005 Posted July 5, 2010 Author Share Posted July 5, 2010 Thanks Ken So if I don't use a WHERE clause, is there any way in which to get it to do something like a "loop". and go through each row and update with a different random code? Thanks Link to comment https://forums.phpfreaks.com/topic/206734-update-multiple-rows-with-random-code/#findComment-1081151 Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 Even in a loop, you would still have to use the where clause. Do you have an auto increment field for each record or some other field that is unique for each record? Here is an example if each record has a unique id field: <?php $q = "select id from people"; $rs = mysql_query($q); while ($rw = mysql_fetch_assoc($rs)) { $uq = "update people set access = " . rand(1111,9999) . " where id = " . $rw['id']; $urs = mysql_query($uq); } ?> Note: untested, no error checking included in the code. Ken Link to comment https://forums.phpfreaks.com/topic/206734-update-multiple-rows-with-random-code/#findComment-1081161 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.