将变量从mysql分配给php

时间:2014-07-27 12:40:05

标签: php mysql email

我正在尝试发送用户名和密码,这些用户名和密码来自我的数据库,通过电子邮件发送给用户。我如何达到目的?我正在使用下面的当前代码,每当收到电子邮件时,我意识到$ username和$ password的地方都是空的,这意味着没有发送数据库值。

<?php

$to = $email;
$subject = "Registered details with BFS ! ";
$message = "Dear $username, as requested , here are your login details. ";
$message .= " Username = $username  Password = $password";
$headers = 'From:noreply@bfs.com';
$sentmail = mail($to,$subject,$message,$headers);

?>

编辑****

完整代码

     <?php 
error_reporting(0);


$email=$_POST['email'];


echo $result['price'];
if($_POST['submit']=='Send')
{
mysql_connect('localhost','root','root') or die(mysql_error);
mysql_select_db('users');
$query="select * from user where email='$email'";
$result=mysql_query($query) or die(error);
if(mysql_num_rows($result))
{
echo "User exists";

$username = mysql_query("select * from user where username = '$username'");
$result = mysql_fetch_assoc($username);

$to = $email;
$subject = "Registered details with BFS ! ";
$message = "Dear $result, as requested , here are your login details. ";
$message .= " Username = $username  Password = $password";
$headers = 'From:noreply@bfs.com';
$sentmail = mail($to,$subject,$message,$headers);
}


else
{
echo "No user exist with this email id";
}
}

?>

1 个答案:

答案 0 :(得分:1)

改变这个:

$username = mysql_query("select * from user where username = '$username'");

进入这个:

$res = mysql_fetch_assoc( $result );
$res = $res['username'];
$username = mysql_query( "select * from user where username = '$res'" );

和此:

$result = mysql_fetch_assoc($username);

进入这个:

$result = mysql_fetch_assoc($username);
$username = $result['username'];
$password = $result['password'];
$result = $result['username'];

如果它不起作用我必须错过一些东西:-)但如果确实如此,用它来了解更多关于逻辑如何工作的内容,之后阅读PDO并使用相反。 : - )