【问题标题】:while loop is only outputing one rowwhile循环只输出一行
【发布时间】:2018-10-09 20:10:29
【问题描述】:

当我在循环内的每个变量之前使用 echo 时,while 循环内的数据工作正常,但在循环外,php 似乎无法识别变量,即使数据库有不止一行,也只打印一次。请帮忙!

    if($process['organdoner']=="Yes" && $processorgan['bloodtype']== $process['bloodtype'] && $processorgan['bodytype']== $process['bodytype']  ){ //with same bloodtype,bodytype,intensivecare
        $to=$process['email'];
        $header= "Organ is urgently needed";
        $phquery="SELECT personprofile.firstname,personprofile.lastname,personprofile.fathername,hospital.hospitalname,hospital.geolocation 
        FROM personprofile,hospital,areaname 
        WHERE areaname.id= hospital.area AND personprofile.hospitaladmission= hospital.id AND areaname.area='$city' AND personprofile.admissionreason= 5";
        $fetchospitalperson=  mysqli_query($link,$phquery);
        $processtable=mysqli_fetch_assoc($fetchospitalperson);
        do{
         $messagebody= "Patient Name: ".$processtable['firstname']." ".$processtable['fathername']." ".$processtable['lastname']."  "."Hospital Name:"; 

        //$hyperlink= new DOMDocument();
        //$hyperlink->loadHTML("<html><body><a href='<".$processtable['geolocation']."'>" .$processtable['hospitalname']."</a></body></html>"); 
         $hyperlink= "<html><body><a href='<".$processtable['geolocation']."'>".$processtable['hospitalname']."</a></body></html>";
         //Name of the person that needs the blood transfusion along with hospital he is staying at,hyperlinked to its location
        }while($processtable=mysqli_fetch_assoc($fetchospitalperson));

    $message= "Dear"." ".$firstname." ".$lastname.",".PHP_EOL .$messagebody.$hyperlink.PHP_EOL; 
    if(isset($sendtoperson)){   
        if(mail($to,$header,$message)){
            echo "Sent";
        }
        else{ echo "Not sent";}
    }
} 

【问题讨论】:

    标签: php sql while-loop


    【解决方案1】:

    您每次循环时都会覆盖变量,因此不要使用x=y,而是使用连接器x .= y

    do{
         $messagebody .= "Patient Name: ".$processtable['firstname']." ".$processtable['fathername']." ".$processtable['lastname']."  "."Hospital Name:"; 
    
         $hyperlink .= "<html><body><a href='<".$processtable['geolocation']."'>".$processtable['hospitalname']."</a></body></html>";
    
    }while($processtable=mysqli_fetch_assoc($fetchospitalperson));
    

    【讨论】:

    • 虽然我不确定只是附加内容会带来任何有意义的东西。
    猜你喜欢
    • 2012-11-15
    • 2019-03-08
    • 2014-07-13
    • 2011-11-02
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 2013-09-28
    相关资源
    最近更新 更多