I would like to know how I can import a CSV file with null values to my MySQL table.
This is the code I use in my PHP to import the CSV file to the MySQL table.
<?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 test(cognome,nome,etichetta,email) VALUES(". $value .")";
if ($this->query($q)) {
$this->state_csv = true;
} else {
$this->state_csv = false;
}
}
}
}
My CSV file has this structure:
Test, Test,,test@test.com
If I import the CSV file with all the data I can import it but the problem is when there is a null value.