Connecting Tech Pros Worldwide Help | Site Map

mysql_fetch_assoc

Newbie
 
Join Date: Nov 2008
Posts: 12
#1: Nov 25 '08
Ok I have a problem! i have a mysql_fetch_assoc that does not recognize the NULL value of the column. however, i want this null to be picked up so i can use it. how can i go about doing that?

Expand|Select|Wrap|Line Numbers
  1. while($row = mysql_fetch_assoc($result)) {
  2.  
  3. echo "<customer_id>" . $row['customer_id'] . "</customer_id>";
  4.  echo "<gender>" . $row['gender'] . "</gender>";//
  5.  echo "<language>" . $row['language'] . "</language>";//
  6.  echo "<birthdate>" . $row['birthdate'] . "</birthdate>";//
  7.  echo "<contact_id>" . $row['contact_id'] . "</contact_id>";//
  8.  echo "<insur1_id>" . $row['insur1_id'] . "</insur1_id>";//
  9.  echo "<insur2_id>" . $row['insur2_id'] . "</insur2_id>";//
  10.  echo "<guardian_id>" . $row['guardian_id'] . "</guardian_id>"; //this value is null
  11.  echo "<hearloss_id>" . $row['hearloss_id'] . "</hearloss_id>";
  12.  echo "<equipment_id>" . $row['equipment_id'] . "</equipment_id>";
  13.  

! get data out of this array but it does not recognize the null fields at all.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,741
#2: Nov 26 '08

re: mysql_fetch_assoc


Hi.

I'm assuming the row is printed, but the NULL field is simply empty?

If that is the case, then that is precisely what is supposed to happen. A NULL is not really a value. It represents that there is no value.
So, when you tell PHP to print a null, it will print nothing.

If you want it to print something, rather than nothing, then you must tell PHP what it is supposed to print. For example:
Expand|Select|Wrap|Line Numbers
  1. while($row = mysql_fetch_assoc($result)) {
  2.   $value = ($row['value'] == null ? "NULL" : $row['value']);
  3.   echo "$value\r\n";
  4. }
  5.  
Which would give you a list of values fetched via a query, substituting null fields with the word "NULL".
Reply