rbarnett Posted November 3, 2010 Share Posted November 3, 2010 I have a user table that holds email addresses and for testing purposes I am trying to replace every email address with an email address defined in an array. I would like to randomly choose an email address from the array and update the table with this address. When I run the following code it randomly chooses a email address from the array but then updates every row with this one email address. Can you someone please let me know what I am doing wrong. Thanks in advance. $query = "SELECT * FROM user '"; $result = mysql_query($query); $input = array('email1', 'email2', 'email3', 'email4', 'email5', 'email6', 'email7'); $rand_keys = array_rand($input, 2); $replaceStr = $input[$rand_keys[0]]; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $rand_keys = ""; $rand_keys = array_rand($input, 2); $replaceStr = $input[$rand_keys[0]]; mysql_query("UPDATE user SET email = '$replaceStr'"); } Link to comment https://forums.phpfreaks.com/topic/217686-updating-mysql-table-with-random-element-from-array/ Share on other sites More sharing options...
freelance84 Posted November 3, 2010 Share Posted November 3, 2010 mysql_query("UPDATE user SET email = '$replaceStr'"); You need to specify the user: mysql_query("UPDATE user SET email = '$replaceStr' WHERE user='what ever the reference to the user field is' "); Link to comment https://forums.phpfreaks.com/topic/217686-updating-mysql-table-with-random-element-from-array/#findComment-1129995 Share on other sites More sharing options...
rbarnett Posted November 3, 2010 Author Share Posted November 3, 2010 Thank you! That makes perfect sense. I added $i = 1; before the while loop and $i++ inside the while loop and added where id = $i to the update query. That works. I really appreciate your help. Link to comment https://forums.phpfreaks.com/topic/217686-updating-mysql-table-with-random-element-from-array/#findComment-1130000 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.