I'm trying to get data from a csv file line by line using PHP, and then insert into Mysql.
Some of the values in the rows of the csv file are empty, and need to be loaded as Null values into Mysql. I can't seems to check these values though. I've tried:
if (strlen($csv[9])==0) {
$follow_up1 = null;
} else {
$follow_up1 = $mysqli->real_escape_string($csv[9]);
}
or:
if (empty($csv[9]) {
$follow_up1 = null;
} else {
$follow_up1 = $mysqli->real_escape_string($csv[9]);
}
or:
if (is_null($csv[9]) {
$follow_up1 = null;
} else {
$follow_up1 = $mysqli->real_escape_string($csv[9]);
}
How can I check if the cell value is empty?
$csv?var_dump($csv)nullwill then not write a null value to the database, as you would need to put the stringNULLwithout quotes into the query. Instead you should use prepared statements, then you can pass null values, and also get rid of thereal_escape_string, and be save(r) against sql injection.