Question

The following snippet of SQL returns the results that I want for any number of entries until I insert a row with the value 0 for cumulative score. Once I do this I get no results returned. cumulativescore is an INT field with no constraints on it.

$stmt = $dbh->prepare("
   select
      cumulativescore
   from 
      reporting
   order by
      cumulativescore asc
");

UPDATE

I think what's happening is that when I use the following code, fetchColumn returns 0 for the first column as it's in ascending order and this breaks my loop. By no results I meant $arr was empty but from this example I have learned that that wasn't the best way to test the query. How can I use fethchColumn and have 0 values? Or do I need to fetch as an associated array and get the value that way? As a sidenote, what's the most appropriate way to test PDO result sets for values/etc?

while($tmp = $stmt->fetchColumn()){
   $arr[] = $tmp;
}
Était-ce utile?

La solution

while(($tmp = $stmt->fetchColumn()) !== false){
   $arr[] = $tmp;
}

three equal signs also check the type of the variable. This is true if the function returns not false exactly. If it returns 0 this is true.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top