简体   繁体   中英

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. Any help? Thanks in advance

With $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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM