gpt4 book ai didi

javascript - 我如何从 PHP fetch assoc 获取结果

转载 作者:行者123 更新时间:2023-11-29 10:49:17 25 4
gpt4 key购买 nike

// 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",
)
);
}

}
);

好吧,我有这个代码来检索密码并直接发送到邮件,它工作得很好,我收到了邮件,但唯一的问题是我得到的密码是“数组”。密码是$admin。有什么帮助吗?提前致谢

最佳答案

使用 $stmt->fetch(PDO::FETCH_ASSOC); 你会得到一行(在本例中是第一行)而不是列值为了获取列值,您应该使用

 $row = $stmt->fetch(PDO::FETCH_ASSOC);
$admin = $row['admin_password'];

如果你返回的值超过一个值,你应该循环查询结果

关于javascript - 我如何从 PHP fetch assoc 获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44039719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com