X Pahadi
X Pahadi

Reputation: 7443

Why does MySQL Query skips null value? How to prevent MySQL skipping the NULL value?

I would like to know why MySQL Query serves different number of columns when some values in NULL or Zero(Integer Case).

For example, I have due as "0" integer (not sting - without Quotes) in some columns. When I make a query, I want to show "0" integer. But that column is skipped.

Can't it return just nothing instead of skipping the NULL value? How Can we overcome this error?

  $sql = "SELECT * FROM `2014-02-20`";
  $query = mysqli_query($con, $sql);
  $row = mysqli_fetch_assoc($query);

 while ($row = mysqli_fetch_assoc($query)){
    while ($this_item = current($row)) {
      echo key($row)."-->".$row[key($row)]."<br />";
      next($row);
    }
    echo "<br />";
  }


?>

I see different results like: Case 1 (with 0 in due column)

qty-->1
amount-->390

Case 2 (with some value in due)

activity-->sales
qty-->1
amount-->25500

Upvotes: 2

Views: 359

Answers (1)

hek2mgl
hek2mgl

Reputation: 157937

Use foreach in the inner loop:

while ($row = mysqli_fetch_assoc($query)){
    foreach($row as $key => $value) {
        echo "$key --> $value <br />";
    }
    echo "<br />";
}

A foreach loop will iterate through the whole $row array regardless of values that might evaluate to false.

Upvotes: 4

Related Questions