将电子邮件发送到从数据库检索到的电子邮件地址由PHP Array中的会话启动到字符串转换?

时间:2016-10-27 02:49:25

标签: php html mysql

我正在尝试在您启动会话时创建系统,然后将电子邮件发送到用户创建帐户的电子邮件。我遇到了一个我试图解决的错误,但我无法做到。这是错误:

  

注意:数组转换为字符串   第37行的C:\ xampp \ htdocs \ LoginSystem \ index.php

我一直在使用代码段,但显然没有正确应用它们:

<?php
    require 'assests/database.php';
    session_start();  
?>
<!DOCTYPE html>
<html>    
<head>    
<title>Welcome to your web app</title>    
<link rel="stylesheet" stype="text/css" href="assests/style.css">    
<link href="https://fonts.googleapis.com/css?family=Comfortaa" rel="stylesheet">    
</head>    
</head>    
<body> 
<div class="header">    
  <a href="index.php"> Your App Name</a>    
</div>      
<?php if( isset($_SESSION['user_id']) ): ?>
<?php
    $sql = 'select email from noodles_gamification where ID = "'.$_SESSION['user_id'].'"';
    $result = $conn->query($sql);
    $email = "";
    // output data of each row
    $row = $result->fetch(PDO::FETCH_ASSOC);
    $email = $row["email"];    
    /*    
    $recipients = array();    
    while($row = mysql_fetch_array($stmt)) {    
        $recipients = $row[0]['email'];    
    } */
    $to = $email;
    $subject = "E-mail subject";
    $body = "E-mail body";
    $headers = "From: noreply@prakashsoft.esy.es" ;
    mail($to, $subject, $body, $headers);
?>
<br /> Welcome you are succesfully loggin in!
<a href="assests/logout.php">Logout?</a>
<?php else : ?>
<h1>Please login or register</h1>
<a href="assests/login.php">login</a> or 
<a href="assests/register.php">Register</a>
<?php endif; ?>
</body>    
</html>

1 个答案:

答案 0 :(得分:0)

尝试更改

`$row = $result->fetch(PDO::FETCH_ASSOC);` 

`$email = $result->fetchAll(PDO::FETCH_COLUMN);`

您的查询看起来像这样

$sql = 'select email from noodles_gamification where ID = "'.$_SESSION['user_id'].'"';
$result = $conn->query($sql);
$email = "";
// output data of each row
$email = $result->fetchAll(PDO::FETCH_COLUMN);

希望这对你有帮助。