我的问题是PHP无法正确执行我想执行的以下SQL命令:
DELETE FROM student
WHERE student.id NOT IN (SELECT studentImport.id FROM studentImport);
从mysql中,我输入sql命令并完美运行,当我使用php擦除所有数据时运行该问题。 想法是在php中运行该sql,因为我在一个新表中进行了导入,如果它们不匹配,则与第一个表进行比较,ID将从第一个表中删除。 这个想法如下:
student:
=============
id | name
1 | Oriol
2 | Ricard
studentImport
=============
id | name
1 | Oriol
结果将是:
student:
=============
id | name
1 | Oriol
studentImport:
=============
id | name
1 | Oriol
我的php代码如下:
class csv extends mysqli {
private $state_csv = false;
public function __construct() {
parent::__construct("localhost","","","");
if ($this->connect_error) {
echo "File to connect Database: ". $this ->connect_error;
}
}
public function import($file=''){
$file = fopen($file, 'r');
while ($row = fgetcsv($file)) {
$value = '"'. implode('","', $row) .'"';
$q = "INSERT INTO studentImport(id,nome) VALUES(". $value .")";
if ($this->query($q)) {
$this->state_csv = true;
$deleteOldStudent = "DELETE FROM student WHERE student.id NOT IN (SELECT studentImport.id FROM studentImport);";
if ($this->query($deleteOldStudent)) {
$this->state_csv = true;
} else {
$this->state_csv = false;
} else {
$this->state_csv = false;
}
}
}
}