How to retrive only one cell from MySQL table with PHP
I'm trying to create a forgotten password form that emails users their password. I'm having a problem, though, with the actual password part. You see, I have the email and comparing the email correct, except whenever I send the email I always get either "Your password is ." or "Your password is Array". I'm using:
$check_email = mysql_num_rows(mysql_query("SELECT email FROM userRecovery WHERE email = '$to'"));
if($check_email == 1){
$qtip = mysql_query("SELECT password FROM userRecovery WHERE email = '$to'");
$theirPassword = mysql_fetch_array($qtip);
Rest of the Code...
}
I used to be able to do this correctly, but I haven't done PHP or MySQL in too long so it's slightly annoying (that,开发者_StackOverflow社区 and I'm at a beginner-intermediate kind of level). I remember having this exact problem, but I don't have the code with me to find out what I did. If you think I left out a detail, please say so.
Any help if appreciated.
$theirPassword
, as you're using it, is an array (as what's being fetched via your mysql_fetch_array
command). Try either $theirPassword['password']
or use just `mysql_result($qtip,'password')``
mysql_fetch_array
returns an array, so if you're using your $theirPassword
it will contain an array. Since you're selecting password from your query, you likely need:
$qtip = mysql_query("SELECT password FROM userRecovery WHERE email = '$to'");
$row = mysql_fetch_array($qtip);
$theirPassword = $row['password'];
What about fetching the email address and the password in one query? SELECT email,password FROM userRecovery WHERE email = '$to'
. Then you can get the password just like in the previous 2 answers ($theirPassword['password']
).
On top of that you may find this blog post about storing passwords in a db useful - http://blog.moertel.com/articles/2006/12/15/never-store-passwords-in-a-database
$sql = "SELECT password FROM userRecovery WHERE email = '$to'";
$password = mysql_result(mysql_query($sql), 0);
精彩评论