Jump to content

updating mysql table with random element from array


rbarnett

Recommended Posts

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'");

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.