Jump to content

Running a randomization script on all of my MySQL records


Recommended Posts

This is a randomization script... basically $altboom is a random text variable which will be entered into a mysql record; it needs to be run on every record in my database. As it stands, this code runs the randomization script one time. I need the random text string line to be run once for each. How can I tell PHP to do this?

 

//define the random text string

$randomtext = ($str1 . " " . $str2 . " " . $str3 . " "  . $str4 . " " . $str5 . " " . $str6 . " " . $str7);

//insert the random text string into the record

mysql_query("UPDATE table SET field = '$altboom' WHERE '*' = '*'");

 

 

well, it works, but the same as it did before. I need the $altboom variable to be "defined" once for each record. As it stands (and with the changes recommended) it puts the same exact random text in each record. How can I make the randomization script be run seperately for each record in the query?

 

I know this may be confusing, but any help would be appreciated.

i don't no what kind of random string you want but you could use something like

$altboom=md5(time());

or for a more random string

$altboom=md5(time()*rand(0,99999);

if you only want numbers just remove the md5 part

and if you wanted something smaller than 32 use sub str

eg

$altboom=substr($altboom,0,15);

for a 15 chr long string

 

Scott.

$query = mysql_query("SELECT * FROM table");

while($row = mysql_fetch_array($query)) {
    $rand = rand(1, 9999);
    $update = mysql_query("UPDATE table set `field` = $rand WHERE `id` = {$row['id']}");
}

I think we are on the wrong track. Let me explain the script a bit more fully:

 

//this block of code defines $str1... there is a similar block of code to define $str2, $str3, etc

srand((float) microtime() * 10000000);

$input = array("fun", "happy", "silly", "wacky", "cool", "lucky", "cool", "cute", "attractive", "great", "unreal", "sweet", "nice");

$rand_keys = array_rand($input, 2);

$str1 = $input[$rand_keys[0]];

 

 

//define the random text string. This needs to happen individually for every MySQL insert

$randomtext = ($str1 . " " . $str2 . " " . $str3 . " "  . $str4 . " " . $str5 . " " . $str6 . " " . $str7);

 

//insert the random text string into the record. Needs some kind of clause to get a new $randomtext for every insert

mysql_query("UPDATE table SET field = '$randomtext' WHERE '*' = '*'");

I think we are on the wrong track. Let me explain the script a bit more fully:

 

well

 

$query = mysql_query("SELECT * FROM table");

while($row = mysql_fetch_array($query)) {
    $i = 0;
    //srand((float) microtime() * 10000000);
    $input = array("fun", "happy", "silly", "wacky", "cool", "lucky", "cool", "cute", "attractive", "great", "unreal", "sweet", "nice");
    //$rand_keys = array_rand($input, 2);
    $randomtext = implode(" ", $input);
    //$str1 = $input[$rand_keys[0]];
    //$update = mysql_query("UPDATE table set `field` = $rand WHERE `id` = {$row['id']}");    //$rand = rand(1, 9999);
    $update = mysql_query("UPDATE table set `field` = '{$andomtext}' WHERE `id` = {$row['id']}");

}

  • 2 weeks later...
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.