警告:非法字符串偏移量php sql

警告:非法字符串偏移量php sql,php,pdo,foreach,Php,Pdo,Foreach,当我使用foreach循环浏览sql请求的结果时,我遇到了一个问题。我得到了以下错误查询警告:使用foreach时字符串偏移量非法: 谁能帮帮我吗,我迷路了 这是我的密码: connexion.php: $bdd = new PDO('mysql:host=localhost;dbname=rep', '******', '******'); sendMail.php include("connexion.php"); require 'PHPMailerAutoload.php'; $sq

当我使用foreach循环浏览sql请求的结果时,我遇到了一个问题。我得到了以下错误查询警告:使用foreach时字符串偏移量非法: 谁能帮帮我吗,我迷路了

这是我的密码:

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';
}
您应该在第7行使用而不是
fetch()

$data = $sql->fetchAll();
foreach ($data as $raw) {
    $destinataire = $raw['destinataire'];
    $atelier = $raw['atelier'];
    $anomalie = $raw['anomalie'];
}
方法
fetch()
只返回结果集中的下一行。因此,另一种选择是在一段时间内使用
fetch()

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

    // rest of your code
}
while ($raw = $sql->fetch()) {
    $destinataire = $raw['destinataire'];
    $atelier = $raw['atelier'];
    $anomalie = $raw['anomalie'];

    // rest of your code
}