Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add random password into my SQL database

Status
Not open for further replies.

Skippie1

Technical User
May 7, 2012
29
ZA
Good day,

I am new to PHP and I am trying to add a random password for my clients when they are verified. The script I am using is as follows:

<?php
function genpwd($cnt)
{
$pwd = str_shuffle('abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@#%$*');

return substr($pwd,0,$cnt);
$password=genpwd(6);
}
echo genpwd(6);

$conn = mysql_connect('localhost','database','password') or die('Iam dying');
mysql_select_db('database_table',$conn);
$string = "insert into `Table` (`password`) values ('$password')";
mysql_query($string,$conn);
mysql_close();
?>

What is happening is that a database entry is created but there is nothing in the password field. Any suggestions??
 
Hi

[tt]function genpwd($cnt)
{
$pwd = str_shuffle('abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@#%$*');

return substr($pwd,0,$cnt);
$password=genpwd(6);
}[/tt]
[ul]
[li]There is no life after an unconditional [tt]return[/tt] - the assignment to $password is never executed. ( Fortunately. Otherwise the interpreter would run into stack overflow error due tu the infinite recursive call.)[/li]
[li]Assigning to the local variable $password inside a function will not be visible to the code outside the function.[/li]
[/ul]


Feherke.
feherke.github.io
 
Sorry, like I said I am new to PHP. How do I fix it so that I can add the random generated password to my database?
 
Also, you probably want to echo the generated $password instead of calling the function again, which would echo another (different) shuffled/random password.
Code:
$password=genpwd(6);
echo $password;
 
Thanks so much, it is working now. Such a small thing....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top