Search code examples
phpmysqlphpmailer

If statement is not working in phpmailer service


I'm setting a phpmailer service in which I'm just sending a link access to some users. The service works perfectly. My problem comes out when I set a IF statement and it doesn't validate. I just want that it can enter to the if validation. The validation to make is when, $result=='s' is true, send the mail, else ($result=='n'), do nothing.

I've researched a lot about this error, obviously not solution found. This is for a mobile app built in ionic 3 with TypeScript and PHP services in Apache server. Also I use a connection to a MySQL DB.

{
<?php

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers');

include('conecction.php');


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'phpMailer/src/Exception.php';
require 'phpMailer/src/PHPMailer.php';
require 'phpMailer/src/SMTP.php';


$id =$request->id;


$counting = count($request->people);

for($i=0;$i<$counting ;$i++){

    $name=$request->people[$i]->name;
    $email=$request->people[$i]->mail;
    $hash=md5($id.$email);

$sql="select assisted from assistance where mail= '$email' and idmeeting = '$id'";

$result = $conn->query($sql);

 if($result == 's'){

$mail = new PHPMailer;
  
$mail->SMTPDebug = 4; 
   
$mail->setFrom('noresponder@Prueba.com', 'Prueba MeetCoomeva');
$mail->addAddress($email, $name); 


$mail->Subject = 'Subject'; 

$mail->Body    = "Message Body "

}
if ($mail->send()) {
 $response['message']="Sent.";
} else { 
   echo "Sending error: " . $mail->ErrorInfo;
}
}
echo json_encode($response);
?>
}

I want the service compose and send the mail when it have made the validation $result=='s'

I getting a 500 server internal error just by this. I just want it sends the mail when the validation is true or do nothing when entered in else.


Solution

  • This isn't enough:

    $sql="select assisted from assistance where mail= '$email' and idmeeting = '$id'";
    
    $result = $conn->query($sql);
    
     if($result == 's'){
    

    $result contains an instance of mysqli_result, not a simple string, so comparing it with s will never match. You need to fetch the row from the result set first, like this:

    $sql="select assisted from assistance where mail= '$email' and idmeeting = '$id'";
    
    $result = $conn->query($sql);
    $row = $result->fetch_row();
    
     if($row[0] == 's'){