简体   繁体   English

我如何从PHP Fetch Assoc获取结果

[英]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.

相关问题 如何从 fetch 中获取 uri? - How can I get the uri from the fetch? 如何在 php 文件中获取获取 function 的路径? - How can I get the path of the fetch function in the php file? 如何替换特定提取的结果? - How can I replace the result of a specific fetch? 如何使用php从sql查询中获取完整结果,然后使用javascript显示该结果 - How Can I get a full result from a sql query using php and then display that with javascript 如何获取结果并将结果传递给反应中的下一个获取? - How can I fetch and pass the result to the next fetch in react? 在reactjs中,我如何从其他文件的fetch()获取响应 - In reactjs how can I get the response from fetch() in other file 我如何从嵌套数组中获取数据并得到结果 - how i can get data from nested array and get the result 如何检查Ajax返回的php脚本的结果 - How can I check on a result from a php script that is returned with ajax 我如何从 fetch 调用 EXpress NoseJs Javascript 中解析这个结果 - How can i parse this result from a fetch call EXpress NoseJs Javascript 如何使 php 从 mysql 的列中获取最后一行,并使结果成为我可以使用 javascript 的变量? - How to make php get last row from a Colum from mysql and make the result a variable that I can take with javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM