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

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.