如何在foreach中使用PDO rowCount()函数?

时间:2016-02-22 22:29:25

标签: php pdo

我需要一些帮助,我有简单的代码,如php中的count行,我使用PDO,

所以我检查rowCount > 0我是否在工作,如果没有其他工作,但我在foreach功能,我在第一步得到真实结果,但在其他我得到无效

所以我认为它在PDO中的功能类似于closeCursor(),但我尝试并且无论如何。也许我做错了?

它是我的代码的一部分

public function saveClinicCalendar($post){
    $daysItm = '';
    $Uid =  $post['Uid'];
    $ClinicId =  $post['ClinicId'];
    $type =  $post['type'];
    $resChck = '';

    foreach($post['objArray'] as $arr){
        foreach($arr['days'] as $days){
            $daysItm = $days.",".$daysItm;
        }

        $daysItm = substr($daysItm, 0, -1);
        $dateTime = $arr['dateTime'];

        $sqlChck = 'SELECT * FROM clinic_weeks WHERE dates = :dates AND Uid = :Uid AND category = :category AND Cid = :Cid AND type = :type';
        $resChck = $this->db->prepare($sqlChck);
        $resChck->bindValue(":dates",$dateTime);
        $resChck->bindValue(":Cid",$ClinicId);
        $resChck->bindValue(":type",$type);
        $resChck->bindValue(":Uid",$Uid);
        $resChck->bindValue(":category",$Uid);

        $resChck->execute();
        $co = $resChck->rowCount();

        if($co > 0){
            /*UPDATE*/

            $sql = 'UPDATE clinic_weeks SET dates = :dates ,time = :time, Cid = :Cid, type  = :type, Uid = :Uid, category = :category   ';
            $res = $this->db->prepare($sql);
            $res->bindValue(":dates",$dateTime);
            $res->bindValue(":time",$daysItm);
            $res->bindValue(":Cid",$ClinicId);
            $res->bindValue(":type",$type);
            $res->bindValue(":Uid",$Uid);
            $res->bindValue(":category",$Uid);

        }else{
            /*INSERT*/

            $sql = 'INSERT INTO clinic_weeks (dates,time, Cid,type,Uid,category) VALUES (:dates,:time, :Cid,:type,:Uid,:category)';
            $res = $this->db->prepare($sql);
            $res->bindValue(":dates",$dateTime);
            $res->bindValue(":time",$daysItm);
            $res->bindValue(":Cid",$ClinicId);
            $res->bindValue(":type",$type);
            $res->bindValue(":Uid",$Uid);
            $res->bindValue(":category",$Uid);
        }

        $res->execute();
        $resChck->closeCursor();

        $resChck = null;

        $daysItm = '';
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:-1)

非常感谢 Barmar ,他建议我给出一个真正的答案。 这是一个代码

 $sql = "INSERT INTO clinic_weeks
       (`timestam`,`time`,dates,Cid,type,Uid,category)
       VALUES
       ('$timestamp','$daysItm','$dateTime','$ClinicId','$type','$Uid','$Uid')
        ON DUPLICATE KEY UPDATE `time` =  '$daysItm' ";

我在那里使用" ON DUPLICATE KEY UPDATE"它的工作完美! 而是一个大代码页面顶部我做了两行代码。