title: Selecting rows where a field is null using PHP PDO prepared statements and MySQL
body: <p>I've been converting an app to use PDO prepared statements rather than mysqli and I'm running into a strange problem. I have some records in the database where it's expected that a field will be null. Not 'null' (string), or '' (empty string), but NULL. I build my queries dynamically, so in the past when I came across a null variable in an object, I'd build the query like this:</p>
  
  <pre><code>WHERE fieldName is null;
  </code></pre>
  
  <p>And would get the expected results when the field was null.</p>
  
  <p>Now with PDO, my queries aren't returning any results and I'm not getting any errors. It just simply isn't returning the records I would expect. When I echo the built queries and run them directly in MySQL I get the expected results, but within the application there are no results returned.</p>
  
  <p>Some of the things I've tried include building queries that look like this:</p>
  
  <pre><code>WHERE fieldName is null;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>WHERE fieldName &lt;=&gt; null;
  </code></pre>
  
  <p>I have also tried the standard prepared statement of: </p>
  
  <pre><code>WHERE fieldName = :fieldName
  </code></pre>
  
  <p>and then binding with these kinds of statements:</p>
  
  <pre><code>$stmt-&gt;bindParam(":$field", $value);
  $stmt-&gt;bindParam(":$field", $value, PDO::PARAM_NULL);
  $stmt-&gt;bindParam(":$field", null, PDO::PARAM_NULL);
  $stmt-&gt;bindValue(":$field", null, PDO::PARAM_NULL);
  $stmt-&gt;bindValue(":$field", null, PDO::PARAM_INT);
  </code></pre>
  
  <p>Any help with this would be greatly appreciated. My PHP version is 5.3.10 and MySQL is 5.5.22. As a side question, I still am not clear on the difference between bindParam and bindValue, so if it makes sense to include in your answer I would really appreciate some clarification on the subject...</p>