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
https://forums.phpfreaks.com/topic/245615-looping-make_seed-function-in-a-foreach/
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?

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);
}

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.