[英]How can i get result from PHP fetch assoc
// Request password
$app->get('/api/admin/forgot', function(Request $request, Response $response){
$email = $request->getParam('email');
$sql = "SELECT admin_password FROM administrators WHERE admin_email = '$email' ";
try{
$db = new db();
$db = $db->connect();
$stmt = $db->query($sql);
$admin = $stmt->fetch(PDO::FETCH_ASSOC);
$db = null;
if($admin == null){
echo json_encode(array(
"errno" => 1,
"message" => "No account",
)
);
return;
}
echo $admin;
require_once "vendor/autoload.php";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "contact@SMS.com";
$mail->FromName = "SAMPLES Management Software";
//To address and name
$mail->addAddress($email);
//Address to which recipient will reply
$mail->addReplyTo("contact@SMS.com", "SAMPLE Management Software");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Password recovery";
$mail->Body = "<h3>Password recovery<h3></br><p>Hi there, your password is '$admin'</p>";
if(!$mail->send()) {
echo json_encode(array(
"errno" => 0,
"message" => "Email not sent.",
)
);
} else {
echo json_encode(array(
"errno" => 0,
"message" => "Email sent.",
)
);
}
} catch(PDOException $e) {
echo json_encode(array(
"errno" => 1,
"feedback" => $e->getMessage(),
"message" => "Error occured",
)
);
}
}
);
Okay so i have this code to retrieve passwords and send to mail directly, it works great and i get the mail but the only problem is i am getting the password as 'array'. 好的,所以我有这段代码来检索密码并直接发送到邮件,它的工作原理很好,我收到邮件,但是唯一的问题是我将密码作为“数组”获取。 The password is $admin. 密码为$ admin。 Any help? 有什么帮助吗? Thanks in advance 提前致谢
With $stmt->fetch(PDO::FETCH_ASSOC);
使用$stmt->fetch(PDO::FETCH_ASSOC);
you get a row ( in this case the first row) and not the column values for get the column values you should use 您得到一行(在本例中为第一行),而不是用于获取应使用的列值的列值
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$admin = $row['admin_password'];
and if you retunr more than a value you shold loop over the query result 如果您重新调整的值超过一个值,则将在查询结果上进行循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.