0

I have a problem when I use foreach loop to browse the result of a sql request i got this error queryWarning: Illegal string offset when i use foreach: can anybody help me please, I'm lost

Here is my code:

connexion.php:

$bdd = new PDO('mysql:host=localhost;dbname=rep', '******', '******');

sendMail.php

include("connexion.php");
require 'PHPMailerAutoload.php';

$sql= $bdd->query('SELECT count(id) as id from inci where retour="0" AND 
atelier="test"');
       $data = $sql->fetch();

$mail = new PHPMailer;

$mail->SMTPDebug = 3;                               

$mail->isSMTP();                                     
$mail->Host = '******';                   
$mail->SMTPAuth = true;                              
$mail->Username = '******';                 
$mail->Password = '*******';                     
$mail->SMTPSecure = 'tls';                            
$mail->Port = 587;                                    
$mail->setFrom('******');
$mail->addAddress('******', 'name');     

$mail->Subject = ' valider';
$mail->Body    = "
<table>
 <thead>
<tr>

                        <th> Destinataire     |</th>
                        <th> Atelier          |</th>
                        <th> Anomalie                                                
</th>

</tr>
 </thead>
 "; 

 foreach($data as $raw) {
                $destinataire=$raw['destinataire'];
                $atelier=$raw['atelier'];
                $anomalie=$raw['anomalie'];




 $mail->Body .="
 <tr>


                        <td>  ".$destinataire."</td>
                        <td>  ".$atelier." </td>
                        <td>  ".$anomalie." </td>

</tr>
</table>

      @endforeach

";
     }
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';


if(!$mail->send()) {
  echo 'Message could not be sent.';
 echo 'Mailer Error : ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

error:

Warning: Illegal string offset 'destinataire' in 
/sendemail/sendEmail.php on line 41

Warning: Illegal string offset 'atelier' in 
/home/sendemail/sendEmail.php on line 42

Warning: Illegal string offset 'anomalie' in 
/home/sendemail/sendEmail.php on line 43

Warning: Illegal string offset 'destinataire' in 
/home/sendemail/sendEmail.php on line 41

Warning: Illegal string offset 'atelier' in 
/home/sendemail/sendEmail.php on line 42

Warning: Illegal string offset 'anomalie' in 
/home/sendemail/sendEmail.php on line 43
Message has been sent
0

1 Answer 1

1

You should use fetchAll() instead of fetch() on line 7:

$data = $sql->fetchAll();
foreach ($data as $raw) {
    $destinataire = $raw['destinataire'];
    $atelier = $raw['atelier'];
    $anomalie = $raw['anomalie'];
}

Method fetch() returns only one next row from a result set. So, another option is to use fetch() in a while cycle:

while ($raw = $sql->fetch()) {
    $destinataire = $raw['destinataire'];
    $atelier = $raw['atelier'];
    $anomalie = $raw['anomalie'];

    // rest of your code
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.