【发布时间】:2016-02-22 22:29:25
【问题描述】:
我需要一些帮助,我有简单的代码,比如 php 中的计数行,我使用 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 = '';
}
}
我做错了什么?
【问题讨论】:
-
你在 foreach 循环中在哪里使用
$item变量? -
我写 foreach 例如我使用它,更重要的是代码在一个 cicle 中,我猜这是我的问题 }
-
究竟是什么在这里不起作用?
-
为什么不先使用
INSERT .. ON DUPLICATE KEY UPDATE而不是先使用SELECT? -
如果我不执行 SELECT,如何首先从 sql 获取项目?是否有其他方法可以比较 sql 数据和我的新数据?