我目前使用类中的一个函数将数据插入数据库,如果每一行都成功插入(来自 csv 文件),则会记录一条消息(logMessage 函数),以显示哪一行成功或不成功。但是,我想要已导入数据库的成功执行的计数。我遇到了一些麻烦
public function insertData($data, $name, $quantity, $date){
try {
$sql = "INSERT INTO `test`(data, name, quantity, date) VALUES(:data, :name, :quantity, :date)";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(":data", $data);
$stmt->bindParam(":name", $name);
$stmt->bindParam(":quantity", $quantity);
$stmt->bindParam(":date", $date);
if($stmt->execute()){
FileProcessor::logMessage("Data imported: <b>$data</b>");
} else {
FileProcessor::logMessage("Not Imported <b>$data</b>");
}
} catch (PDOException $e){
FileProcessor::logMessage("Error: " . $e->getMessage());
}
}