barbs75 Posted March 31, 2008 Share Posted March 31, 2008 Hi guys, i have a piece of code in a function called 'notify_password()' which is part of the process of resetting a forgotten password with email notification for my user authentication system for my website. i am using a simple MYSQL query to retrieve the email address from my database table given the username of the user, so i can store the email in a short variable and send them an email with the new random password. When i perform the script, using echo's, the query is giving the email value as 'resource#7' and not the email address?? and i dont understand why? i have read through all my php and mysql textbooks and cant figure out the problem. Here is the code for the function notify_password(): function notify_password($username) // notify the user that their password has been changed { $sql = "SELECT email FROM customer WHERE username='$username'"; $check_user = mysql_query($sql); $user_exist = mysql_num_rows($check_user); if(!$user_exist) { throw new Exception('Could not find email address.'); } else if ($user_exist==0) { throw new Exception('Could not find email address.'); // given email address not in db } else { $useremail = $check_user; echo 'the value of email is'.$useremail; $yoursite = 'www.go4home.co.uk'; $webmaster = 'Craig Barber website Manager'; $youremail = '[email protected]'; $subject = "You have successfully reset your $yoursite password..."; $message = "Your go4home password has been changed to $password \r\n" ."Please change it next time you log in. \r\n Thanks, $webmaster"; if (mail($useremail, $subject, $message, "From: $yoursite <$youremail>\nX-Mailer:PHP/" . phpversion())) return true; else throw new Exception('Could not send email.'); } } The value of the number of rows that are found for the query is 1, meaning that the query is picking up the record, bt not retrieving the email address. If anyone could have a quick peek and see if i have missed something really STUPID out, that would be great, any help received with open arms! cheers Craig Link to comment https://forums.phpfreaks.com/topic/98860-solved-php-mysql-query-problems-retrieving-email-from-table-help/ Share on other sites More sharing options...
paul2463 Posted March 31, 2008 Share Posted March 31, 2008 try this one <?php function notify_password($username) // notify the user that their password has been changed { $sql = "SELECT email FROM customer WHERE username='$username'"; $result = mysql_query($sql); // you missed out these bits $row = mysql_fetch_assoc($result); $check_user = $row['email']; $user_exist = mysql_num_rows($result); if(!$user_exist) { throw new Exception('Could not find email address.'); } else if ($user_exist==0) { throw new Exception('Could not find email address.'); // given email address not in db } else { $useremail = $check_user; echo 'the value of email is'.$useremail; $yoursite = 'www.go4home.co.uk'; $webmaster = 'Craig Barber website Manager'; $youremail = '[email protected]'; $subject = "You have successfully reset your $yoursite password..."; $message = "Your go4home password has been changed to $password \r\n" ."Please change it next time you log in. \r\n Thanks, $webmaster"; if (mail($useremail, $subject, $message, "From: $yoursite <$youremail>\nX-Mailer:PHP/" . phpversion())) return true; else throw new Exception('Could not send email.'); } } ?> Link to comment https://forums.phpfreaks.com/topic/98860-solved-php-mysql-query-problems-retrieving-email-from-table-help/#findComment-505849 Share on other sites More sharing options...
barbs75 Posted April 1, 2008 Author Share Posted April 1, 2008 Hey Paul, thanks for your reply, i was reading on the mysql_fetch_assoc(), couldn't grasp it, because i haven't had to use it before...so when you do a SELECT from the database you need to use the fetch_assoc() to pull the record that is selected from the query, this returns an array storing each element of data from the record...ie (username, email, surname,) etc and then you refer to the element of the array you want and store it in a short variable, is that correct yeah? just so that i understand.... cheers for your help, you have really cleared it up for me! Craig Link to comment https://forums.phpfreaks.com/topic/98860-solved-php-mysql-query-problems-retrieving-email-from-table-help/#findComment-506364 Share on other sites More sharing options...
paul2463 Posted April 1, 2008 Share Posted April 1, 2008 you can use either "mysql_fetch_array()" or "mysql_fetch_assoc()" both do the same job they just create the return array in a different method. lets say you are pulling "fistname" lastname" and "email" from the database using mysql_fetch_array() you reference them using the array key number such as $row = mysql_fetch_array($result); $first = $row[0]; $last = $row[1]; $email = $row[2]; great if you are only pulling a couple of things and you know the order you requested the information in as that is the array key to use. personally I like to use the other method because then instead of trying to remember the order you asked for them in you just need to know the name of the column you asked for them from. such as:- a different method. lets say you are pulling "fistname" lastname" and "email" from the database using mysql_fetch_assoc() you reference them using the array key name such as $row = mysql_fetch_assoc($result); $first = $row['firstname']; $last = $row['lastname']; $email = $row['email']; hope that helps in some small way but the thing you forgot in this case was to get the information from the result resource $sql = "SELECT email FROM customer WHERE username='$username'"; $result = mysql_query($sql); // you missed out these bits $row = mysql_fetch_assoc($result); Link to comment https://forums.phpfreaks.com/topic/98860-solved-php-mysql-query-problems-retrieving-email-from-table-help/#findComment-506386 Share on other sites More sharing options...
barbs75 Posted April 1, 2008 Author Share Posted April 1, 2008 Hey Paul, cheers for all that, that made it crystal clear, i got a bit confused by it in the book i was reading, so thanks a lot for that mate take it easy Craig Link to comment https://forums.phpfreaks.com/topic/98860-solved-php-mysql-query-problems-retrieving-email-from-table-help/#findComment-506486 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.