Hi, i have a form where the user enters in their email address, what i want is to look in my database for their email, retreive their password and send it in an email to their address.
$db_object = DB::connect($datasource, TRUE); /* assign database object in $db_object, if the connection fails $db_object will contain the error message. */
if(DB::isError($db_object)) { die($db_object->getMessage()); // If $db_object contains an error print out the } // error and exit.
$db_object->setFetchMode(DB_FETCHMODE_ASSOC);
// begin actual query process
$sql="SELECT password FROM yoursite_users WHERE email = '$_REQUEST[email]';"; $passObject=$db_object->query($sql); if(DB::isError($passObject)){ print "Error: table 'yoursite_users' does not exist or bad SQL query!<br/>nn"; echo $sql; }else if($passObject->numRows()==0){ print "No user with that email address exists.nn"; }else{ $passArray=$passObject->fetchRow(); $to=$_REQUEST['email']; $subject="Lost password retrieval."; $contents="Your password is ".$passArray['password']; $headers = 'From: lost_password@' . $_SERVER['SERVER_NAME'] . "rn" . 'X-Mailer: PHP/' . phpversion();
// lines longer than 70 chars can mess up the mail() function $contents=wordwrap($contents, 70);
$emailSent=mail($to, $subject, $contents, $headers); if($emailSent){ print "Password sent to your email address!nn"; }else{ print "Error: email could not be sent.nn"; }//end if email sent }//end if error or numrows
$db_object->disconnect();
?> [/code]
This script takes a single form element named "email" and looks for the password for the user with that email address. If the table "yoursite_users" doesn't exist or the SQL query gets messed up somehow, it points this out and does nothing else. If there is no user with that email address, there will be 0 rows in the $passObject array, and it will print out an appropriate message and do nothing more. If neither of these two conditions occur, it fires off an email to the address provided containing the password.
@gaston9x19Apr 17.2005 — #Have fun with substr() and strlen() lol... Sorry, I hate working with flat database files :-P Never have very much. You might try using explode() to separate everything into an array if your separators are identical, make it a little easier to work with. I dunno.