tags:

views:

108

answers:

2

Hey guys,

So I'm using the PHP function fgetcsv to parse a .csv file (comma separated, fields are enclosed by "") and import it to a MySQL. The problem is, a certain field is always empty, even though it should sometimes contain "0". So I'm guessing the function consider "" and "0" as null values, but it's quite problematic for me. Any ideas?

EDIT - The code :

$handle = fopen("/home/simon/projet-web/Quebec.csv", "r");

if ($handle)
{      
  while($line = fgetcsv($handle, 0, ",", "\""))
  {
    if ($line[55] === "0") 
      echo $line[55] . " / " . $line[4] . "<br />";
  [...]
+1  A: 

Use Identical Comparision Operator.

Crozin
+1 Only fair, as you got there first. :-)
middaparka
Already tried - I just added the code. Doesn't change anything, nothing get through this condition (like if the function considered this value as null and that the value it gave to "line[55]").
Afrosimon
Array of values (from single line) will always contains strings. So you don't have 0 as integer, but as a string. You should check whether: ... === '' returns true - then you have a empty column. ... === '0' returns true if column contains zero
Crozin
Yeah, true, I added the necessary "". But the problem is, even if there are some "0" value in the file, fgetcsv ignore them and consider them null, and so the condition is never met.
Afrosimon
+1  A: 

You should be able to tell the difference between a zero entry and empty field using the following...

if($currentRow[$currentIndex] === 0)

...to check for the presence of a zero. (The same logic applies when using functions that return either a zero or false, such as strpos - in that instance, you'd check for !== false, etc.)

As Crozin says, this is the identical comparison operator, which checks for type as well as value equivalence.

middaparka
Yeah well like in my comment to Crozin, this doesn't looks like it help (even though it should). There's definitely something about fgetcsv which elude me.
Afrosimon