【问题标题】:Warning: Illegal string offset php sql [duplicate]警告:非法字符串偏移 php sql [重复]
【发布时间】:2017-09-05 08:41:10
【问题描述】:

当我使用 foreach 循环浏览 sql 请求的结果时遇到问题,我收到此错误 queryWarning: Illegal string offset when I use foreach: 谁能帮帮我,我迷路了

这是我的代码:

连接.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';
}

错误:

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

【问题讨论】:

    标签: php pdo foreach


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      相关资源
      最近更新 更多