简体   繁体   English

为什么执行查询后我的代码结果为空白,在这一行$ out = $ query-> result_array();

[英]Why is the result of my code blank after executing query, on this line $out = $query->result_array();

this is the image of database table. 这是数据库表的图像。 在此处输入图片说明

I'm new at codeigniter PHP 我是Codeigniter PHP的新手

public function ForgotPassword($email)
{ 

    $this->db->select('email');
    $this->db->from('member');
    $this->db->where('email', $email);
    $query=$this->db->get();
    $out = $query->result_array();

    if (count($out) > 0) {
        $email = $out[0]['email'];
        return array('status'=>1, 'email'=>$email); 
    } else {
        return array('status'=>0, 'msg'=>'email not found');
    }
}

This is my model function., this line giving me blank result $out = $query->result_array(); 这是我的模型函数。此行给我空结果$out = $query->result_array(); I don't know why, it should give me email address so that i can proceed and send email. 我不知道为什么,它应该给我电子邮件地址,以便我可以继续发送电子邮件。 I want to send password to user using forgot password function. 我想使用忘记密码功能向用户发送密码。

public function ForgotPassword()
{
    $email = $this->input->post('email');
    $findemail = $this->MY_model->ForgotPassword($email);

    if ($findemail['status'] == 1) {
        $this->MY_model->sendpassword($findemail);
    } else {
        echo " $email not found, enter correct email id";
    }
}

And this is my controller function. 这是我的控制器功能。 Here I'm getting blank value of $findemail . 在这里,我得到了$findemail空值。 I don't know why. 我不知道为什么

public function sendpassword($data)
{
    $email = $data['email'];

    $query1=$this->db->query("SELECT * from member where email = '".$email."' ");
    $row=$query1->result_array();
    if ($query1->num_rows() > 0) {
        $passwordplain = "";
        $passwordplain  = rand(10000000,99999999);
        $newpass['password'] = md5($passwordplain);
        $this->db->where('email', $email);
        $this->db->update('member', $newpass);
        $mail_message='Dear '.$row[0]['username'].','. "\r\n";
        $mail_message.=' Your <b>Password</b> is 
                       <b>'.$passwordplain.'</b>'."\r\n";

        require 'C:\xampp\htdocs\phpmailer\class.phpmailer.php';
        require 'class.phpmailer.php';

        $mail = new PHPMailer;
        $mail->IsSendmail();
        $mail->isSMTP();
        $mail->SMTPAuth = true;
        $mail->Host = "smtp.gmail.com";
        $subject = 'Testing Email';
        $mail->AddAddress($email);
        $mail->IsMail();
        $mail->From = 'vishal@gmail.com';
        $mail->FromName = 'admin';
        $mail->IsHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $mail_message;
        $mail->Send();
        if (!$mail->send()) {
            echo "Failed to send password";
        } else {
            echo "Password sent ! check your mail";
        }
    } else {
        echo "Email not found!!!!";
    }
}

And also upper model function , it's not sending email. 还有上层模型功能,它不发送电子邮件。 I'm using phpmailer to sending email. 我正在使用phpmailer发送电子邮件。 Please help. 请帮忙。

If your model name is 'MY_model' then write construct function in your controller file and load the model into it as shown below. 如果您的模型名称为“ MY_model”,则在控制器文件中写入构造函数,然后将模型加载到其中,如下所示。

function __construct() {
        parent::__construct();
        $this->load->model('my_model');
    }

And modify your ForgotPassword function of controller as below. 然后按如下所示修改控制器的ForgotPassword函数。

public function ForgotPassword()
{
$email = $this->input->post('email');
$findemail = $this->my_model->ForgotPassword($email);

if ($findemail['status'] == 1) {
    $this->my_model->sendpassword($findemail);
} else {
    echo " $email not found, enter correct email id";

}
}

I hope this helps. 我希望这有帮助。

I have made a function that checks if email exists in db. 我做了一个检查数据库中是否存在电子邮件的功能。 It is named CheckUser(). 它名为CheckUser()。 Then in ForgotPassword() you call CheckUser() ($email must be given as argument). 然后在ForgotPassword()中,调用CheckUser()(必须以$ email作为参数)。

<?php
    public function CheckUser($email)
    {
        $response = $bdd->prepare('SELECT email FROM member WHERE email = :email');
        $response->execute([
          "email" => $$email
        ]);
        $data = $response->fetchAll();

        return $data[0];
    }

    public function ForgotPassword($email)
    {
        // here you call our new function

** UDPATE BELOW CALLING OF CHECKUSER FUNCTION ** ** CHECKUSER FUNCTION调用下面的UDPATE **

        $out = $this->CheckUser($email);

        // sizeof will return the lenght of the returned data from our CheckUser() function
        if (sizeof($out) > 0){
            $email = $out[0]['email'];
            return array('status'=>1, 'email'=>$email); 
        }
        else {
            return array('status'=>0, 'msg'=>'email not found');
        }

    }

Let me know if it works. 让我知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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