Jump to content

looping make_seed function in a foreach


BigTime

Recommended Posts

Im trying to assign and update table rows with a random 9 character string but am receiving the error : Fatal error: Cannot redeclare make_seed() (previously declared in ...

 

How can I clear/reset the value before the next loop :confused:

 

else {
	 		while ($data=mysql_fetch_array($users)){
	 		$user=$data[user];

	 		$usersarray = array($users);

	 			foreach ($usersarray as $key => $u){
				//clear pass
				$token = "";
				srand();

				//generate new password
				$password_length = 9;

				function make_seed() {
  					list($usec, $sec) = explode(' ', microtime());
  					return (float) $sec + ((float) $usec * 100000);
				}

				srand(make_seed());

				$alfa = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM!@#$%^&*(){}[]";

				for($i = 0; $i < $password_length; $i ++) {
  					$token .= $alfa[rand(0, strlen($alfa))];
					}    

				// insert password
				// setup SQL statement  
	   			$SQL = " UPDATE data SET pass='$token' WHERE user='$m1' ";
        			
        		// execute SQL statement
    	    		$makeit = mysql_db_query($userdb,"$SQL",$usercid);

        		// check for error
    	    			if (!$makeit) { echo("ERROR: " . mysql_error() . "\n$SQL\n");
					} 

Link to comment
Share on other sites

Hi Shawn, thank you.

 

The entire purpose of the foreach loop is to create a unique entry for each record.  If I move it out of the loop, then how do I get a new and unique entry for each record?

 

I was hoping there was a way to reset/clear the function make_seed() declared value at the top or bottom of the loop

 

am I going about this the wrong way?

Link to comment
Share on other sites

You need to move the function declaration out of the loop.  You can only define a function once (as the error tells you), but you have it in a loop to declare it multiple times.  Move this to the very bottom of your page outside of any loops:

 

function make_seed() {
   list($usec, $sec) = explode(' ', microtime());
   return (float) $sec + ((float) $usec * 100000);
}

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.