title: SQL NULL return gives empty string in php using odbc
body: <p>I have a database with two identical tables (one containing values, and the other contains values/users). When reading the values I wish to take the two values and place them onto one row. In certain cases (for some columns) there is only a value and not a value/users-value, in these cases I wish for the returned value to be null.</p>
  
  <p>To make things a bit more clear, here is an example;</p>
  
  <pre>
  Value table (Table1)
  
      |  id  |  CPU  | Memory |  Name  |
      |------+-------+--------+--------|
      |  1   |   45  |   25   |  Comp1 |
      |  2   |   25  |  12.5  |  Comp2 |
  
  Value/Users table (Table2)
  
      |  id  |  CPU  | Memory |  Name  |
      |------+-------+--------+--------|
      | 1_A  |  22.5 |  12.5  |        |
      | 2_A  |   5   |   2.5  |        |
  </pre>
  
  <p>I do not for instance need to know the value of Name divided by the number of users. In order to differentiate these values from the rest, I have a table named <code>colInfo</code> that specifies which values DO exist in the second table using the variable <code>columnName</code>. (Note: I cannot modify the structure of the database).</p>
  
  <p>When retrieving the data I use the following SQL Query;</p>
  
  <pre><code>SELECT t1.[NAME] as Value, t2.[UserValue]
  FROM (SELECT * FROM TABLE1 WHERE ID = 1) t1
  left join (SELECT [ID],
  CASE(SELECT COUNT(*) FROM colInfo WHERE [columnName] LIKE 'NAME')
      WHEN 0 THEN NULL
             ELSE [NAME]
  END as [UserValue] from t2 on LEFT(t2.Id,LEN(t2.Id)-1) = LEFT(t1.Id,LEN(t1.Id)-1)
  </code></pre>
  
  <p>(The reasoning behind the sub-select and the left join goes along with the fact that I cannot change the database structure)</p>
  
  <p>When running this query in SQL Management Studio, I get the correct result;</p>
  
  <pre>
      | ID | Value | UserValue |
      |----+-------+-----------|
      | 1  | Comp1 |   NULL    |
  </pre>
  
  <p>But when running it from a php-site using odbc I get, instead of <code>NULL</code>, an empty string. I use the following code;</p>
  
  <pre><code>$query = $this-&gt;sqlFactory-&gt;getComputerStats($params,$ids);
  $result = odbc_exec($this-&gt;conn, $query);
  for ($j=1; $j&lt;=$no_rows; $j++) { 
    odbc_fetch_row($result, $j);
    if(!is_null(odbc_result($result,"UserValue"))){
      echo odbc_result($result,"UserValue");
    }
  }
  </code></pre>
  
  <p>Using Developer Tools this will give me an empty string. If I instead <code>echo $query</code> and test this in the SQL environment I do get a <code>NULL</code> value.</p>
  
  <p>How come the <code>odbc_result</code> will not parse the result as <code>null</code>?</p>
  
  <p><strong>EDIT:</strong> According to the documentation of odbc_result it should return <code>null</code> when the value of the SQL Query returns NULL. As can be seen in the <a href="http://www.php.net/manual/en/function.odbc-result.php" rel="nofollow">documentation.</a></p>